【问题标题】:I am getting this error while executing update query in visual studio 2012在 Visual Studio 2012 中执行更新查询时出现此错误
【发布时间】:2015-08-13 10:29:18
【问题描述】:

'=' 附近的语法不正确

我的代码是

"update staff_Tables 
 set emp_id='" + txtEmployeeID.Text + "' , 
     emp_sal='" + txtEmpSal.Text + "', 
     emp_name='" + txtEmployeeName.Text + "',
     emp_Deignation='" + txtDesignation.Text + "',
     Gender='" + cboGender.Text + "',
     contactno='" + txtContact.Text + "',
     Address='" + rtbAddress.Text + "',
     Joining_Date='" + txtjoindate.Text + "' 
 where txtid=" + txtid.Text, sqlcon.getCon());

【问题讨论】:

  • 欢迎来到stackoverflow。我已经编辑了您的问题以改进格式。请阅读How to Ask
  • emp_sal列的数据类型是什么

标签: sql sql-server sql-server-2008 visual-studio-2012 sql-update


【解决方案1】:

您的 .Text 值中可能有一个单引号,通过将它们加倍来修复,例如:

Address='" + Replace(rtbAddress.Text, "'", "''") + "' vb
Address='" + rtbAddress.Text.Replace("'", "''") + "' #c

但是,是的,您可以使用这种更新数据库的方法进行 sql 注入。

【讨论】:

    【解决方案2】:

    首先,您不应该像这样连接 sql 查询。 您的查询极易受到Sql Injection 攻击。

    始终使用stored proceduresparameterized queries

    第二,你尝试调试了吗?从列名来看,Emp_IdEmp_Salcontactno 可能是数字数据而不是字符串,因此值周围的 ' 是错误的。

    您的查询应如下所示:

    "update staff_Tables 
     set emp_id = @emp_id, 
         emp_sal = @emp_sal, 
         emp_name = @emp_name,
         emp_Deignation = @emp_Deignation,
         Gender = @Gender,
         contactno = @contactno,
         Address = @Address,
         Joining_Date = @Joining_Date 
     where txtid = @txtid"
    

    然后像这样将参数添加到SqlCommand.Parameters 集合中:

    cmd.Parameters.Add("@emp_id, SqlDBType.Int).Value = txtEmployeeID.Text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多