2013-04-11 83 views
0

我有此代码由自己研发制造, 它没有返回错误,更新(S)一些在文本框输入的数据,但不是所有的领域更新vb.net缺陷中的sql命令?

我检查正在更新比较近场码它的文本框,不 更新。

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

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 
+1

我们没有你的数据库或数据,所以我们无法运行你的代码 - 所以也许你可以给我们一些提示 - 例如你说“一些领域” - 也许告诉我们哪些工作,哪些工作't? – 2013-04-11 08:27:14

+3

另外,您需要查看使用参数化查询 – 2013-04-11 08:27:35

+0

哪些字段没有更新? – Steve 2013-04-11 08:29:44

回答

0

一些故障诊断建议:

尝试这样的模式:

 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") 

广场第二行的断点并使用Text Visualizer查看SQL。您也可以复制它并使用其他查询工具来处理它并找出错误。

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

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

或做每个:OE_Location_Code =“” & cmbLocationCode.Text.replace( “ '”, “ ''”)

这将转换 “弗雷德的房间” 到 “弗雷德' 的房间”