停止CommandText插入将重复项添加到SQL数据库中

时间:2015-05-20 15:04:28

标签: sql vb.net vba

我有一个显示客户信息的表单,允许用户在最初输入后编辑和添加更多信息。我编写的代码删除了实例并使用不同的GUID再次添加它,但是重复了新添加的实例。

我正在寻找代码,只在数据库中保存一个这样的实例,而不是重复。

这是我的代码:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim strGUID As String
    strGUID = System.Guid.NewGuid().ToString
    If CNameText.Text <> "" And AddressTextBox.Text <> "" And PhoneText.Text <> "" And DateTextBox.Text <> "" And CashierText.Text <> "" And EmailText.Text <> "" And SubjectText.Text <> "" And DepartmentTextBox.Text <> "" And EnquiryText.Text <> "" Then
        cn.Open()
        cmd.CommandText = "DELETE from Enquiries where UniqueID = '" & GUIDTextBox.Text & "' "
        cmd.ExecuteNonQuery()
        cn.Close()

        GUIDTextBox.Text = strGUID

        cn.Open()
        cmd.CommandText = "INSERT into Enquiries (CName, Address, Phone, Date, Email, Cashier, Department, Subject, Enquiry, Note, UniqueID, FutDate) values ('" & CNameText.Text & "', '" & AddressTextBox.Text & "', '" & PhoneText.Text & "', '" & DateTextBox.Text & "', '" & EmailText.Text & "', '" & CashierText.Text & "', '" & DepartmentTextBox.Text & "',  '" & SubjectText.Text & "', '" & EnquiryText.Text & "', '" & NoteTextBox.Text & "', '" & GUIDTextBox.Text & "', '" & FutDateTextBox.Text & "')"
        cmd.ExecuteNonQuery()
        cn.Close()

        CNameText.Text = ""
        AddressTextBox.Text = ""
        DateTextBox.Text = ""
        PhoneText.Text = ""
        CashierText.Text = ""
        EmailText.Text = ""
        SubjectText.Text = ""
        DepartmentTextBox.Text = ""
        EnquiryText.Text = ""
        NoteTextBox.Text = ""
        GUIDTextBox.Text = ""

        cn.Open()
        EnquiriesTableAdapter.Fill(Database1DataSet1.Enquiries)
        cmd.ExecuteNonQuery()
        cn.Close()


        MsgBox("Enquiry Successfully Updated")
    End If
End Sub

0 个答案:

没有答案