2016-09-19 258 views
0

如何在MySQL中檢查重名?例如,在這種情況下,該產品不應該是相同的。如何在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

嘗試使用'select'語句,如果它存在,或者不那麼就插入該名稱是否可用 – Spiderman

+0

SQL注入肯定先搜索名稱。第二順序,但仍然。連接是錯誤的 – Drew

回答

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