(LocalDb)\ Server_name在Visual Studio 2017中的连接字符串中显示错误

时间:2019-01-31 06:20:47

标签: mysql sql-server visual-studio ado.net

我正在使用ADO.net在Visual Studio 2017中制作一个简单的控制台应用程序。我正在使用SQL Server Management Studio来管理和创建表,并仅使用ADO.Net连接到数据库并插入,修改或删除数据库中的数据。

这是我写的:

string cs = "Data Source=(LocalDb)\LocalSERVER;Initial Catalog=PlayerAuction;Integrated Security=True";

SqlConnection conn = new SqlConnection(cs);

SqlCommand cmd = new SqlCommand("INSERT INTO PLAYERS VALUES(@PlayerName,@BattingAvg,@BowlingAvg)", conn);

当我使用ADO.Net时,我正在写一个连接字符串,在数据源中,我提到(LocalDb)\ LocalSERVER作为我的LocalDb实例。它抛出一个错误。我尝试在工具中连接数据库并在其中复制了连接字符串,但效果不佳。

这是我得到的错误:

enter image description here

我已经正确地重新检查了连接字符串。

这是数据库连接:

enter image description here

连接字符串,如下所示:

enter image description here

请帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

反斜杠引起了问题。尝试之一:

string cs = @"Data Source=(LocalDb)\LocalSERVER;Initial Catalog=PlayerAuction;Integrated Security=True";

string cs = "Data Source=(LocalDb)\\LocalSERVER;Initial Catalog=PlayerAuction;Integrated Security=True";