0

我的代码返回错误说

违反主键约束“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
4

3 回答 3

2

检查表 tblOfficeEquipmentProfile 的主键

基于主字段,您可以更新其余字段,意味着删除主键字段的更新

于 2013-04-11T06:22:24.773 回答
1

根据您的主要字段 OE_ID 检查使用位置

myCommand = New SqlCommand("更新 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 = @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_备注

其中 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)
于 2013-04-11T07:16:08.867 回答
1

嗨,这个查询更新表中的所有数据,所以如果你有一些唯一的 ID,你不能用相同的 ID 更新,我认为你需要在这个查询中添加 Where 语句。

于 2013-04-11T06:17:45.630 回答