尝试打开()sql服务器时出错。

时间:2022-09-01 15:46:55

I am new to net developing and have managed to work my way through a lot of questions I have had just by looking through the forums.

我是网络开发新手,通过浏览论坛,我已经设法解决了很多问题。

It appears that the issue that I am having is something that a number of others have had I have found that they are all different and just haven't for the life of me been able to work through it.

我所面临的问题似乎是很多人都遇到过的问题,我发现他们都是不同的,只是在我的一生中都没能解决这个问题。

I am trying to insert player registration details into database but when I try to invoke the wcf server it am met with the exception type on my conn.Open():

我试图将玩家注册细节插入到数据库中,但是当我尝试调用wcf服务器时,会遇到conn.Open()上的异常类型:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code.

“System.Data.SqlClient”类型的一个例外。发生在System.Data SqlException异常”。但是在用户代码中没有处理dll。

In addition I am using the build it sql server and the connection string used is one from properties on the database. I am not too sure how to proceed.

此外,我正在使用build it sql server,使用的连接字符串来自数据库上的属性。我不太确定该怎么做。

public string playerRegistration(playerDetails playerInfo)
{            
    string Message;
    using (SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\\Users\\Daniel\\documents\\visual studio 2013\\Projects\\Prac4\\WebApplication1\\App_Data\\ADODatabase.mdf;Integrated Security=True"))
    {
        conn.Open();

        using (var cmd = new SqlCommand("INSERT into Player(pid, pfname, plname, pphone, paddress, pdob) VALUES (@pid, @pfname, @plname, @pphone, @paddress, @pdob)", conn))
        {
            cmd.Parameters.AddWithValue("@pid", playerInfo.Pid);
            cmd.Parameters.AddWithValue("@pfname", playerInfo.Pfname);
            cmd.Parameters.AddWithValue("@plname", playerInfo.Plname);
            cmd.Parameters.AddWithValue("@pphone", playerInfo.Pphone);
            cmd.Parameters.AddWithValue("@paddress", playerInfo.Paddress);
            cmd.Parameters.AddWithValue("@pdob", playerInfo.Pdob);

            int result = cmd.ExecuteNonQuery();
            if (result == 1)
            {
                Message = " Details inserted successfully";
            }
            else
            {
                Message = " Details not inserted successfully";
            }
            conn.Close();
            return Message;
        }
    }
}

2 个解决方案

#1


5  

Make sure to use @".." (a verbatim string literal) with connection strings to avoid simple escaping mistakes.

确保使用@".."带有连接字符串的字串,以避免简单的转义错误。

The code shown with "..\v.." contains a vertical tab escape which produces an invalid connection string. There is no compiler error because the string literal is syntactically valid although the resulting string is incorrect.

用“.\v.. .”显示的代码包含一个垂直选项卡转义,它生成无效的连接字符串。没有编译器错误,因为字符串文字在语法上是有效的,尽管结果字符串是不正确的。

Recommended fix with a verbatim string literal and elimination of double slashes:

建议使用逐字字符串文字和消除双斜杠:

@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Users\Daniel.."

Alternative fix (note the \\v):

备选修复(注意\v):

"Data Source=(LocalDB)\\v11.0;AttachDbFilename=c:\\Users\\Daniel.."

#2


0  

The problem is in your connection string

问题在于您的连接字符串

"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\\Users\\Daniel\\documents\\visual studio 2013\\Projects\\Prac4\\WebApplication1\\App_Data\\ADODatabase.mdf;Integrated Security=True"

Search the internet to find the required format for SQL Server. You do not need an MDF file name, here's a helpful link:

搜索internet以找到SQL Server所需的格式。您不需要MDF文件名,这里有一个有用的链接:

https://www.connectionstrings.com/sql-server/

https://www.connectionstrings.com/sql-server/

#1


5  

Make sure to use @".." (a verbatim string literal) with connection strings to avoid simple escaping mistakes.

确保使用@".."带有连接字符串的字串,以避免简单的转义错误。

The code shown with "..\v.." contains a vertical tab escape which produces an invalid connection string. There is no compiler error because the string literal is syntactically valid although the resulting string is incorrect.

用“.\v.. .”显示的代码包含一个垂直选项卡转义,它生成无效的连接字符串。没有编译器错误,因为字符串文字在语法上是有效的,尽管结果字符串是不正确的。

Recommended fix with a verbatim string literal and elimination of double slashes:

建议使用逐字字符串文字和消除双斜杠:

@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Users\Daniel.."

Alternative fix (note the \\v):

备选修复(注意\v):

"Data Source=(LocalDB)\\v11.0;AttachDbFilename=c:\\Users\\Daniel.."

#2


0  

The problem is in your connection string

问题在于您的连接字符串

"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\\Users\\Daniel\\documents\\visual studio 2013\\Projects\\Prac4\\WebApplication1\\App_Data\\ADODatabase.mdf;Integrated Security=True"

Search the internet to find the required format for SQL Server. You do not need an MDF file name, here's a helpful link:

搜索internet以找到SQL Server所需的格式。您不需要MDF文件名,这里有一个有用的链接:

https://www.connectionstrings.com/sql-server/

https://www.connectionstrings.com/sql-server/