Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 命名SQL序列';s连接字符串_C#_Sql_.net_Connection_Connection String - Fatal编程技术网

C# 命名SQL序列';s连接字符串

C# 命名SQL序列';s连接字符串,c#,sql,.net,connection,connection-string,C#,Sql,.net,Connection,Connection String,使用Visual Studio 2015 ans SQL Server 2014 我尝试了各种方法,包括使用“[]”、使用双反斜杠和“@”,但它仍然会给出无法识别的转义序列错误。还有其他解决办法吗 SqlCommand cmd = new SqlCommand("Insert into Products(Id,Name,[Description],Price,IsAvailable) Values(@Id, @Name, @Description, @Price, @IsAvailable)",

使用Visual Studio 2015 ans SQL Server 2014

我尝试了各种方法,包括使用“[]”、使用双反斜杠和“@”,但它仍然会给出无法识别的转义序列错误。还有其他解决办法吗

SqlCommand cmd = new SqlCommand("Insert into Products(Id,Name,[Description],Price,IsAvailable) Values(@Id, @Name, @Description, @Price, @IsAvailable)", "Data Source=.\MSSQLSERVER1;Initial Catalog=ProductDB;Integrated Security=True");

用字符串的前导@对@字符进行转义

SqlCommand cmd = new SqlCommand(@"Insert into Products(Id,Name,[Description],Price,IsAvailable) Values(@Id, @Name, @Description, @Price)", yourConnection);

一旦您知道如何正确地转义连接字符串(使用@或\),SqlCommand的构造函数就不会接受连接字符串,如下所示:

var conn=new SqlConnection(@"Data Source=.\MSSQLSERVER1;Initial Catalog=ProductDB;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Insert into Products(Id,Name,[Description],Price,IsAvailable) Values(@Id, @Name, @Description, @Price, @IsAvailable)", conn);


参考@Robert McKee的答案。您需要从命令中取出连接字符串。我没有看到它在那里。即使你连接起来,这些值看起来也不正确。@name应该是一个参数吗?字符串应为“value”
var conn=new SqlConnection("Data Source=.\\MSSQLSERVER1;Initial Catalog=ProductDB;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Insert into Products(Id,Name,[Description],Price,IsAvailable) Values(@Id, @Name, @Description, @Price, @IsAvailable)", conn);