【问题标题】:Update sql command in vb.net flaws?更新 vb.net 中的 sql 命令有缺陷吗?
【发布时间】:2013-04-11 08:20:08
【问题描述】:

我有这个代码是我自己研究的, 它不返回错误,它会更新在文本框中输入的一些数据,但不会更新所有字段

我检查正在更新的字段附近的代码,以将其与未更新的文本框进行比较 更新。

但我看不出有什么区别,它只是没有更新所有字段,只有一些字段

   Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim myCommand As SqlCommand
    Try

        'update command
        sqlconn.Open()

        myCommand = New SqlCommand(
          "UPDATE tblOfficeEquipmentProfile SET OE_Category = '" & cmbCategory.Text
& "',OE_SubCategory = '" & cmbSubCategory.Text
& "', OE_Name = '" & txtName.Text
& "', OE_User = '" & txtUser.Text
& "', OE_Brand = '" & cmbBrand.Text
& "', OE_Model = '" & cmbModel.Text
& "', OE_Specs = '" & txtSpecs.Text
& "', OE_SerialNo = '" & txtSerialNo.Text
& "', OE_PropertyNo = '" & txtPropertyNo.Text
& "', OE_MacAddress = '" & txtMacAddress.Text
& "', OE_Static_IP = '" & txtStaticIp.Text
& "', OE_Vendor = '" & cmbVendor.Text
& "', OE_PurchaseDate = '" & txtPurchaseDate.Text
& "', OE_WarrantyInclusiveYear = '" & cmbWarrantyInclusiveYear.Text
& "', OE_WarrantyStatus = '" & txtWarrantyStatus.Text
& "', OE_Status = '" & txtStatus.Text
& "', OE_Dept_Code = '" & cmbDeptCode.Text
& "', OE_Location_Code = '" & cmbLocationCode.Text
& "', OE_Remarks ='" & cmbRemarks.Text
& "' WHERE OE_ID = '" & txtOEID.Text & "'", sqlconn)
' ^^  (edited to separate lines for ease of viewing )
        myCommand.ExecuteNonQuery()
        MessageBox.Show("Office Equipment Profile Successfully Updated Records")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

【问题讨论】:

  • 我们没有您的数据库或数据,所以 us 无法运行您的代码 - 所以也许您可以给我们一些提示 - 例如你说“一些领域”——也许告诉我们哪些有效,哪些无效?
  • 另外,您需要考虑使用参数化查询
  • 哪些字段没有更新?
  • 只更新的字段是OE_Name、OE_Specs、OE_SerialNo、OE_PropertyNo,其余没有更新我的主键是OE_ID
  • @ivandiglasan 你确实意识到编写这样的代码是对 SQL 注入攻击的邀请,对吧?

标签: sql vb.net winforms sqlcommand


【解决方案1】:

一些故障排除建议:

试试这样的模式:

        Dim SQL As String = "UPDATE STaff Set Initials='RCH' WHERE Initials = 'RCH'"
        myCommand = New SqlCommand(SQL, sqlconn)
        Dim iCnt As Integer = myCommand.ExecuteNonQuery()
        MessageBox.Show("Office Equipment Profile Successfully Updated " & iCnt & " Records")

在第二行放置一个断点并使用文本可视化器查看 SQL。您也可以复制它并使用其他一些查询工具来处理它并查找错误。

另外,捕获更改的记录数(上面的 iCnt)并进行一些 QA 和/或调试。

注入:虽然您的项目可能不会受到注入攻击,但您可以通过不确保 .Text 值不会破坏 SQL 来踩到自己。例如,如果任何 .Text 包含撇号,则 SQL 将失败。您可以编写一个函数将 ' 替换为 '' 并且您会很安全。

或者每个都做:OE_Location_Code = '" & cmbLocationCode.Text.replace("'","''")

这会将“Fred's Room”转换为“Fred's Room”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2019-01-13
    • 2017-05-26
    • 2020-06-06
    • 2011-01-16
    相关资源
    最近更新 更多