0

我有保存按钮和更新按钮的代码,但是有没有办法可以将这两个命令组合在一个按钮中?

例如,当我单击“保存/更新按钮”时,如果它是新记录,它将保存在数据库中,如果系统发现数据库中已有记录并保存已编辑的数据,它将更新

保存按钮代码

Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"
    Try
        Dim myCommand As New SqlCommand
        sqlconn.Open()
        myCommand = New SqlCommand("INSERT INTO tblOfficeEquipmentProfile(OE_Category,OE_SubCategory,OE_ID,OE_Name,OE_User,OE_Brand,OE_Model,OE_Specs,OE_SerialNo,OE_PropertyNo,OE_MacAddress,OE_Static_IP,OE_Vendor,OE_PurchaseDate,OE_WarrantyInclusiveYear,OE_WarrantyStatus,OE_Status,OE_Dept_Code,OE_Location_Code,OE_Remarks) VALUES('" & cmbCategory.Text & "','" & cmbSubCategory.Text & "','" & txtOEID.Text & "','" & txtName.Text & "','" & txtUser.Text & "','" & cmbBrand.Text & "','" & cmbModel.Text & "','" & txtSpecs.Text & "','" & txtSerialNo.Text & "','" & txtPropertyNo.Text & "','" & txtMacAddress.Text & "','" & txtStaticIp.Text & "','" & txtVendor.Text & "','" & txtPurchaseDate.Text & "','" & txtWarrantyInclusiveYear.Text & "', '" & txtWarrantyStatus.Text & "','" & txtStatus.Text & "','" & cmbDeptCode.Text & "','" & cmbLocationCode.Text & "','" & txtRemarks.Text & "')", sqlconn)
        myCommand.ExecuteNonQuery()
        MessageBox.Show("Office Equipment Profile Successfully Added")
        sqlconn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

更新按钮的代码(注意:但我的更新按钮仍然有一些错误仍在尝试修复)

Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim myCommand As New 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 = '" & txtVendor.Text & "', OE_PurchaseDate = '" & txtPurchaseDate.Text & "', OE_WarrantyInclusiveYear = '" & txtWarrantyInclusiveYear.Text & "', OE_WarrantyStatus = '" & txtWarrantyStatus.Text & "', OE_Status = '" & txtStatus.Text & "', OE_Dept_Code = '" & cmbDeptCode.Text & "', OE_Location_Code = '" & cmbLocationCode.Text & "', OE_Remarks ='" & txtRemarks.Text & "' WHERE OE_ID ='" & txtOEID.Text & "'", sqlconn)
        Dim iCnt As Integer = myCommand.ExecuteNonQuery()
        MessageBox.Show("Office Equipment Profile Successfully Updated  " & iCnt & "  Records")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
4

3 回答 3

0

编写一个存储过程来检查记录是否存在。如果是,请更新它。否则,插入它。

从您的 vb.net 代码中调用此存储过程。此外,更改您的 .net 代码,以便将查询参​​数发送到您的存储过程。

于 2013-04-18T02:16:27.733 回答
0

您的问题是关于 SO 的一个常年最喜欢的问题:如何更新现有行或插入它?答案总是错误的。

执行此操作的标准 SQL 没有if语句。标准的方法是插入不存在约束的行,然后更新它:

insert into T values (...)
where not exists (
  select 1 from T as t 
  where T.key = t.key
)

可选择检查行数,仅在为 0 时更新

update T set ... 
where T.key = value

如果您不从 中检查行数insert,则update是多余的。

最好将所有这些都放在一个存储过程中,但即使不这样做,您也应该能够将两个语句放在一个准备好的语句中,并将其附加到 GUI 中的一个按钮上。

于 2013-04-18T06:18:07.800 回答
0

您可以创建一个包含状态的枚举:

 Enum DataState
      Editing
      Adding
      None
 End Enum

然后设置一个类级变量:

 private mDataState as DataState

然后根据您是添加还是编辑记录进行设置,然后在 Save_Click 子例程中使用 If-Then。

于 2013-04-18T01:51:44.513 回答