C# Sql Server 2005插入查询中的单引号和双引号

C# Sql Server 2005插入查询中的单引号和双引号,c#,asp.net,sql-server,sql-server-2005,C#,Asp.net,Sql Server,Sql Server 2005,地址文本框中有单引号和双引号。如何将其插入数据库。我正在使用SQL2005。 我的代码如下 str = "exec sp_cust_reg '" + customer.Cust_Id + "','" + customer.Cust_Name + "','" + customer.Gender + "','" + customer.Acc_no + "','" + customer.Address + "','" + customer.Pin_no + "','" + customer.Phone

地址文本框中有单引号和双引号。如何将其插入数据库。我正在使用SQL2005。 我的代码如下

str = "exec sp_cust_reg '" + customer.Cust_Id + "','" + customer.Cust_Name + "','" + customer.Gender + "','" + customer.Acc_no + "','" + customer.Address + "','" + customer.Pin_no + "','" + customer.Phone_no + "','" + customer.Mobile_no + "','" + customer.Email + "','" + customer.Authorise + "'";
exec sp_cust_reg 'C7','George Joseph','Male','0001-212123','jo"hn's house','515151','04862787896','8888888888','johnyqw@gmail.com','N'
地址是乔的家

其文本可视化工具如下所示

str = "exec sp_cust_reg '" + customer.Cust_Id + "','" + customer.Cust_Name + "','" + customer.Gender + "','" + customer.Acc_no + "','" + customer.Address + "','" + customer.Pin_no + "','" + customer.Phone_no + "','" + customer.Mobile_no + "','" + customer.Email + "','" + customer.Authorise + "'";
exec sp_cust_reg 'C7','George Joseph','Male','0001-212123','jo"hn's house','515151','04862787896','8888888888','johnyqw@gmail.com','N'
我曾经

string sql = str.Replace("\'", " ");.
然后我得到

exec sp_cust_reg  C7 , George Joseph , Male , 0001-212123 , jo"hn s house , 515151 , 04862787896 , 8888888888 , johnyqw@gmail.com , N 

您将
'
转义为
'


因此,不要使用
str.Replace(“\”,”)
使用
str.Replace(“'”,“”)
一个词:不要这样做!

改用参数化查询——这些查询更安全(无SQL注入),更易于处理,性能也更好

SqlCommand cmd = new SqlCommand("dbo.sp_cust_reg", _connection);
cmd.CommandType = CommandType.StoredProcedure;

// add parameters and their values
cmd.Parameters.Add("@CustID", SqlDbType.Int).Value = customer.Cust_Id;
cmd.Parameters.Add("@Cust_Name", SqlDbType.VarChar, 100).Value = customer.Cust_Name;
 ..... and so on - define all the parameters!

_connection.Open();
cmd.ExecuteNonQuery();
_connection.Close();
你也可以使用 对于给定值,请使用以下参数的AddWithValue()

SqlCommand cmd = new SqlCommand("dbo.sp_cust_reg",_connection);
cmd.CommandType= CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@TheDate",customer.Cust_Id);
cmd.Parameters.AddWithValue("@Cust_Name",customer.Cust_Name);

_connection.Open();
cmd.ExecuteNonQuery();
_connection.Close();

我之所以要使用AddWithValue,是因为您显式地设置了sqldb.type,SQL在传递时准确地知道dbtype。

@Petar..字符串是exec sp_cust_reg'C7'、'Johny Joseph'、'Male'、'0001-124578'、'john's house'、'666666666666'、'888888888'、'5555555555'、'johnyqw@gmail.com“,”N“在您的代码之后,它将成为exec sp_cust_reg”“C7”“,”Johny Josep“h”、“男性”、“0001-124578”、“约翰之家”、“666666”、“88888888888”、“55555555”等johnyqw@gmail.com'',''N''否!您只想替换值中的
'
,即customer.Cust\u Name.replace('',''),等等。这是一种不好的方法-参数化SQL在这里是一个更好的解决方案。@marc\s…我的插入查询在远程Web服务中。我只是将插入查询作为字符串传递给插入(字符串s)关于我的Web服务。@DavidJohn:糟糕的设计……这是一个用于SQL注入的大而敞开的仓库门。如果我必须维护它,我会扔掉
Insert
方法,创建一个新的方法,其中1)要调用的存储过程的名称,以及b)要在调用中使用的
SqlParameter
列表……@marc_s……我是初学者……传递t错误吗他将查询作为字符串插入my webservice中的一个方法?@DavidJohn:查询字符串可能会被操纵,远程Web服务可能会在没有任何验证/检查的情况下执行它。这会打开攻击的大门。这不是推荐的做法-最好使用特定的参数化查询,并为单独查询(作为
SqlParameter
instances)——这对性能更好、更安全