我在vb.net和使用mysql数据库时遇到问题。
我已经有一个保存按钮代码,但是我的问题是我需要检测重复的数据。我尝试搜索它,但是它不能与我的代码一起使用,始终存在错误或警告。
我的问题是通过此代码将代码放入检测重复条目的地方?

con = New MySqlConnection
con.ConnectionString = "server = localhost; database=db; uid=root;pwd="
Dim reader As MySqlDataReader
Try
    con.Open()
    Dim query As String
    query = "insert into paraiso_dbms2.client_t(FIRSTNAME,MIDDLENAME,LASTNAME,GENDER,BIRTHDATE,AGE,STREET,BARANGAY,CITY,ZIPCODE,PHONE_NUMBER) VALUES('" & txtfirstname.Text & "','" & txtmidname.Text & "','" & txtlastn.Text & "','" & gender & "','" & dpbd.Text & "','" & txtage.Text & "','" & txtstreet.Text & "','" & txtbrgy.Text & "','" & txtcity.Text & "','" & txtzipcode.Text & "','" & txtphone.Text & "')"
    cmd = New MySqlCommand(query, con)
    reader = cmd.ExecuteReader
    MessageBox.Show("Saved Sucessfully!")
    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally
    con.Dispose()
End Try
load_table()

最佳答案

您应该在下面更新当前的插入代码

con = New MySqlConnection
con.ConnectionString = "server = localhost; database=db; uid=root;pwd="
Try
    con.Open()
    Dim query As String
    query = "insert into paraiso_dbms2.client_t(FIRSTNAME,MIDDLENAME,LASTNAME,GENDER,BIRTHDATE,AGE,STREET,BARANGAY,CITY,ZIPCODE,PHONE_NUMBER) VALUES('" & txtfirstname.Text & "','" & txtmidname.Text & "','" & txtlastn.Text & "','" & gender & "','" & dpbd.Text & "','" & txtage.Text & "','" & txtstreet.Text & "','" & txtbrgy.Text & "','" & txtcity.Text & "','" & txtzipcode.Text & "','" & txtphone.Text & "')"
    cmd = New MySqlCommand(query, con)
    reader = cmd.ExecuteNonQuery
    MessageBox.Show("Saved Sucessfully!")
    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally
    con.Dispose()
End Try
load_table()


以及如何检查记录是否存在,因为没有唯一的号码或电子邮件,您可以将电话号码与电话号码文本框进行比较

con = New MySqlConnection
con.ConnectionString = "server = localhost; database=db; uid=root;pwd="
Try
    con.Open()
    Dim query As String
    query = "select PHONE_NUMBER from paraiso_dbms2.client_t where PHONE_NUMBER='" & txtphone.Text & "'"
    cmd = New MySqlCommand(query, con)
   dim count as string = cmd.ExecuteScalar
    MessageBox.Show(count)
    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
Finally
    con.Dispose()
End Try


甚至我不确定这是否对您有用,因为我没看过完整的代码(如果病了)给您新的

10-08 15:12