如何检查 MySQL 中的重复名称?例如,在这种情况下,该产品不应该是相同的。
Dim SDA As New MySqlDataAdapter
Dim dbSource As New BindingSource
Try
cn.Open()
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert purchase.category set id_cat='" & txtID.Text & "', cat_product='" & txtNama.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show("Succes")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Dispose()
End Try
Load_AddCatPro()
txtID.Clear()
txtNama.Clear()
End Sub
最佳答案
我有这个问题的解决方案,请查看。
Try
If txtNama.Text = "" Then
MessageBox.Show("Isi Category Product", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With txtID
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
cn.Open()
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from purchase.category where cat_product= '" & txtNama.Text & "'"
rd = cmd.ExecuteReader
If rd.HasRows Then
MsgBox("Duplicate Entry Found", MsgBoxStyle.Critical)
cn.Close()
Else
cn.Close()
cn.Open()
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert purchase.category set id_cat='" & txtID.Text & "', cat_product='" & txtNama.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show("Data Tersimpan")
'frmCustomer.DataGridViewCust.Refresh()
cn.Close()
Load_AddCatPro()
txtID.Clear()
txtNama.Clear()
End If
关于mysql - 如何检查 MySQL 数据库中的重复名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39564944/