2013-04-11 66 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( 「 '」, 「 ''」)

這將轉換 「弗雷德的房間」 到 「弗雷德' 的房間」