我的代码返回错误说
违反PRIMARY KEY约束'PK_tblOfficeEquipmentProfile'。无法在对象'tblOfficeEquipmentProfile'中插入重复键。
这是我的代码:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim sqlconn As New SqlClient.SqlConnection
sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
"Database = EOEMS;integrated security=true"
Dim myCommand As SqlCommand
'parametrized update sql command
Try
sqlconn.Open()
myCommand = New SqlCommand("UPDATE tblOfficeEquipmentProfile SET OE_Category = @OE_Category, OE_SubCategory = @OE_SubCategory, OE_ID = @OE_ID, OE_Name = @OE_Name, OE_User = @OE_User, OE_Brand = @OE_Brand, OE_Model =@OE_Model, OE_Specs =@OE_Specs, OE_SerialNo =@OE_SerialNo, OE_PropertyNo = @OE_PropertyNo, OE_MacAddress = @OE_MacAddress, OE_Static_IP = @OE_Static_IP, OE_Vendor = @OE_Vendor, OE_PurchaseDate =@OE_PurchaseDate, OE_WarrantyInclusiveYear=@OE_WarrantyInclusiveYear, OE_WarrantyStatus=@OE_WarrantyStatus,OE_Status=@OE_Status,OE_Dept_Code=@OE_Dept_Code,OE_Location_Code=@OE_Location_Code,OE_Remarks=@OE_Remarks", sqlconn)
myCommand.Parameters.Add("@OE_Category", cmbCategory.Text)
myCommand.Parameters.Add("@OE_SubCategory", cmbSubCategory.Text)
myCommand.Parameters.Add("@OE_ID", txtOEID.Text)
myCommand.Parameters.Add("@OE_Name", txtName.Text)
myCommand.Parameters.Add("@OE_User", txtUser.Text)
myCommand.Parameters.Add("@OE_Brand", cmbBrand.Text)
myCommand.Parameters.Add("@OE_Model", cmbModel.Text)
myCommand.Parameters.Add("@OE_Specs", txtSpecs.Text)
myCommand.Parameters.Add("@OE_SerialNo", txtSerialNo.Text)
myCommand.Parameters.Add("@OE_PropertyNo", txtPropertyNo.Text)
myCommand.Parameters.Add("@OE_MacAddress", txtMacAddress.Text)
myCommand.Parameters.Add("@OE_Static_IP", txtStaticIp.Text)
myCommand.Parameters.Add("@OE_Vendor", cmbVendor.Text)
myCommand.Parameters.Add("@OE_PurchaseDate", txtPurchaseDate.Text)
myCommand.Parameters.Add("@OE_WarrantyInclusiveYear", cmbWarrantyInclusiveYear.Text)
myCommand.Parameters.Add("@OE_WarrantyStatus", txtWarrantyStatus.Text)
myCommand.Parameters.Add("@OE_Status", txtStatus.Text)
myCommand.Parameters.Add("@OE_Dept_Code", cmbDeptCode.Text)
myCommand.Parameters.Add("@OE_Location_Code", cmbLocationCode.Text)
myCommand.Parameters.Add("@OE_Remarks", cmbRemarks.Text)
myCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
MsgBox("Successfully Updated Records")
End Try
End Sub
答案 0 :(得分:2)
检查表tblOfficeEquipmentProfile
的主键基于主要文件,您可以更新其余字段,表示删除主键文件的更新
答案 1 :(得分:1)
您好此查询更新表中的所有数据,因此如果您有一些ID唯一您无法更新使用相同的ID,我认为您需要在此查询中添加Where语句。
答案 2 :(得分:1)
根据您的主要字段OE_ID
检查使用位置myCommand = New SqlCommand(“UPDATE tblOfficeEquipmentProfile SET OE_Category = @OE_Category,OE_SubCategory = @OE_SubCategory,OE_Name = @OE_Name,OE_User = @OE_User,OE_Brand = @OE_Brand,OE_Model = @ OE_Model,OE_Specs = @ OE_Specs,OE_SerialNo = @ OE_SerialNo,OE_PropertyNo = @OE_PropertyNo,OE_MacAddress = @OE_MacAddress,OE_Static_IP = @OE_Static_IP,OE_Vendor = @OE_Vendor,OE_PurchaseDate = @ OE_PurchaseDate,OE_WarrantyInclusiveYear = @ OE_WarrantyInclusiveYear,OE_WarrantyStatus = @ OE_WarrantyStatus,OE_Status = @ OE_Status,OE_Dept_Code = @ OE_Dept_Code,OE_Location_Code = @ OE_Location_Code,OE_Remarks = @ OE_Remarks
其中OE_ID = @OE_ID“,sqlconn)
myCommand.Parameters.Add("@OE_Category", cmbCategory.Text)
myCommand.Parameters.Add("@OE_SubCategory", cmbSubCategory.Text)
myCommand.Parameters.Add("@OE_ID", txtOEID.Text)
myCommand.Parameters.Add("@OE_Name", txtName.Text)
myCommand.Parameters.Add("@OE_User", txtUser.Text)
myCommand.Parameters.Add("@OE_Brand", cmbBrand.Text)
myCommand.Parameters.Add("@OE_Model", cmbModel.Text)
myCommand.Parameters.Add("@OE_Specs", txtSpecs.Text)
myCommand.Parameters.Add("@OE_SerialNo", txtSerialNo.Text)
myCommand.Parameters.Add("@OE_PropertyNo", txtPropertyNo.Text)
myCommand.Parameters.Add("@OE_MacAddress", txtMacAddress.Text)
myCommand.Parameters.Add("@OE_Static_IP", txtStaticIp.Text)
myCommand.Parameters.Add("@OE_Vendor", cmbVendor.Text)
myCommand.Parameters.Add("@OE_PurchaseDate", txtPurchaseDate.Text)
myCommand.Parameters.Add("@OE_WarrantyInclusiveYear", cmbWarrantyInclusiveYear.Text)
myCommand.Parameters.Add("@OE_WarrantyStatus", txtWarrantyStatus.Text)
myCommand.Parameters.Add("@OE_Status", txtStatus.Text)
myCommand.Parameters.Add("@OE_Dept_Code", cmbDeptCode.Text)
myCommand.Parameters.Add("@OE_Location_Code", cmbLocationCode.Text)
myCommand.Parameters.Add("@OE_Remarks", cmbRemarks.Text)