gpt4 book ai didi

mysql - 如何检测 vb.net 和 mysql 数据库中的重复条目

转载 作者:行者123 更新时间:2023-11-29 21:15:06 24 4
gpt4 key购买 nike

我在 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

即使我不确定这是否适合你,因为我没有看到整个代码,如果没有的话我会给你新的

关于mysql - 如何检测 vb.net 和 mysql 数据库中的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com