[英]Update sql command in vb.net flaws?
我自己研究了這段代碼,沒有錯誤,它更新了文本框中輸入的某些數據,但不是所有字段
我檢查了正在更新的字段附近的代碼,以將其與未更新的文本框進行比較。
但我看不到區別,它只是不更新所有字段,僅更新某些字段
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
一些故障排除建議:
嘗試這樣的模式:
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)並進行一些質量檢查和/或調試。
注入:雖然您的項目可能不會受到注入攻擊,但是您可以通過確保.Text值不會破壞SQL來增強自身能力。 例如,如果任何.Text包含撇號,則SQL將失敗。 您可以編寫一個函數將“替換為”,這樣就可以安全了。
或執行每個操作:OE_Location_Code ='“&cmbLocationCode.Text.replace(”'“,”''“)
這會將“弗雷德的房間”轉換為“弗雷德的房間”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.