如何检查MySQL数据库中的重复名称?

时间:2016-09-19 03:30:47

标签: mysql vb.net

如何检查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

form

1 个答案:

答案 0 :(得分:0)

我有解决此问题的方法,请查看此内容。

    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

enter image description here