命名SQL序列中存在无法识别的转义序列错误';s连接字符串
本文关键字:错误 转义序列 字符串 连接 识别 SQL 存在 命名 | 更新日期: 2023-09-27 18:26:10
使用Visual Studio 2015和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的构造函数在您找到如何正确地转义连接字符串(使用@或'')后都不会接受连接字符串,如下所示:
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);
或
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);
用字符串上的前导@转义@字符。
SqlCommand cmd = new SqlCommand(@"Insert into Products(Id,Name,[Description],Price,IsAvailable) Values(@Id, @Name, @Description, @Price)", yourConnection);