如何防止在数据库中输入重复记录?

时间:2017-02-21 05:53:05

标签: vb.net

这是我在Visual Basic上的代码中的一部分。 当我尝试输入已经存在于数据库中的记录时,该程序应该提示我存在重复...

这是我的代码:

程序添加记录

             recSet.Open("SELECT * FROM Prod ", connection, 3, 3)
             recSet.AddNew()
             recSet.Fields("prodSKU").Value = prodSKU.Text
             recSet.Fields("prodName").Value = prodName.Text
             recSet.Fields("prodSRP").Value = prodSRP.Text
             recSet.Fields("prodDesc").Value = prodDesc.Tex 
             recSet.Fields("prodUnit").Value = comboUnit.Text
             recSet.Fields("ClassDisc").Value = classDisc.Text
             recSet.Fields("ProdCateg").Value = comboCtgry.Text

                recSet.Update()
                recSet.Close()
                MsgBox("Records added succesful!", MsgBoxStyle.Information, "Success!")
                'COMOB BOXES'
                comboUnit.Items.Clear()
                comboCtgry.Items.Clear()

                recSet.Open("SELECT DISTINCT prodUnit from Prod", connection, 3, 3)
                Do While Not recSet.EOF
                    comboUnit.Items.Add(recSet.Fields("prodUnit").Value)
                    recSet.MoveNext()
                Loop
                recSet.Close()
                recSet.Open("SELECT DISTINCT prodCateg from Prod", connection, 3, 3)
                Do While Not recSet.EOF
                    comboCtgry.Items.Add(recSet.Fields("prodCateg").Value)
                    recSet.MoveNext()
                Loop
                recSet.Close()

                prodSKU.Text = ""
                prodName.Text = ""
                prodSRP.Text = ""
                prodDesc.Text = ""
                classDisc.Text = ""
                comboCtgry.Text = ""
                comboUnit.Text = ""

1 个答案:

答案 0 :(得分:0)

  

答案已更新,因为上一个答案是针对SQL数据库的,但@DatNoobPotatoPCGuy表示他的数据库是MS Access数据库。

Dim con As OleDbConnection = New OleDbConnection()
Dim cmd As OleDbCommand = Nothing
Dim numberOfRecords as Int = 0;
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=pathToDatabase"
con.Open()
cmd = New OleDbCommand("Select count(*) From Prod where prodSKU = @prodSKU AND prodName = @prodName AND prodSRP = @prodSRP AND prodDesc = @prodDesc AND prodUnit = @prodUnit AND ClassDisc = @ClassDisc AND ProdCateg = @ProdCateg", con)
With cmd
        .Parameters.AddWithValue("@prodSKU", prodSKU.Text)
        .Parameters.AddWithValue("@prodName", prodName.Text)
        .Parameters.AddWithValue("@prodSRP", prodSRP.Text)
        .Parameters.AddWithValue("@prodDesc", prodDesc.Text)
        .Parameters.AddWithValue("@prodUnit", comboUnit.Text)
        .Parameters.AddWithValue("@ClassDisc", classDisc.Text)
        .Parameters.AddWithValue("@ProdCateg", comboCtgry.Text)
End With
numberOfRecords = cmd.ExecuteScalar()
If numberOfRecords <> 0 Then
   MsgBox("Row(s) in database with following data are already exist!",MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Are you trying to insert same record?")
   Exit Sub
Else
   'Your code to insert data
End If
  

试试此代码,请告诉我们您的结果。

     

注意:我从未使用过MS Access数据库并且根本不知道它,但正如我在本期中看到的那样,没有人真正帮助过你,所以我尽我所能,因此对我的错误感到非常抱歉:)