2016-09-19 5 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

は、名前は確かに – Spiderman

+0

SQLインジェクション使用可能な場合、その後の挿入か存在するかどう 'SELECT'文を使用して最初の名前を検索してみてください。まだ2番目の注文です。コンカットは間違った方法です – 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

関連する問題