【问题标题】:Single and double quotes in Sql Server 2005 insert querySql Server 2005 插入查询中的单引号和双引号
【发布时间】:2011-11-14 05:59:44
【问题描述】:

地址文本框中有单引号和双引号。如何插入数据库。我正在使用 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 + "'";

地址是乔恩的家

它的Text Visualizer如下...

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 

【问题讨论】:

    标签: c# asp.net sql-server sql-server-2005


    【解决方案1】:

    一句话:不要这样做!

    改用参数化查询 - 这些查询更安全(无 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();
    

    【讨论】:

    • @marc_s....我的插入查询在远程 Web 服务中。我只是将插入查询作为字符串传递给我的网络服务的 Insert(string S)。
    • @DavidJohn:糟糕的设计......这是一个用于 SQL 注入的 BIG WIDE OPEN BARN DOOR。如果我必须保持这一点,我会抛弃 Insert 方法并创建一个新方法,其中包含 1)要调用的存储过程的名称,以及 b)要在调用中使用的 SqlParameter 列表....
    • @marc_s...我是初学者...将插入查询作为字符串传递给我的 Web 服务中的方法是否错误?
    • @DavidJohn:查询字符串可以被操纵是一个风险,远程网络服务可能会在没有任何验证/检查的情况下执行它。打开攻击之门。 推荐的做法 - 最好有特定的参数化查询,并单独指定查询的参数(如 SqlParameter 实例) - 性能更好,更安全
    【解决方案2】:

    您也可以使用Parameterized queries 用于给出值,使用 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。

    【讨论】:

      【解决方案3】:

      你将' 转为''

      所以不要使用str.Replace("\'", " "),而是使用str.Replace("'", "''")

      【讨论】:

      • @Petar..字符串是 exec sp_cust_reg 'C7','Johny Joseph','Male','0001-124578','john's house','666666','88888888888',' 5555555555','johnyqw@gmail.com','N' 在你的代码之后它变成 exec sp_cust_reg ''C7'',''Johny Joseph'',''Male'',''0001-124578'',''约翰的房子'',''666666'',''88888888888'',''5555555555'',''johnyqw@gmail.com'',''N''
      • 不!您只想替换值中的 ',即 customer.Cust_Name.Replace("'", "''") 等。
      • 这是一个糟糕的方法 - 参数化 SQL 在这里是一个更好的解决方案。
      猜你喜欢
      • 2023-03-25
      • 2013-03-24
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 2021-01-16
      • 1970-01-01
      • 2010-10-21
      相关资源
      最近更新 更多