当我尝试将数据插入数据库时,它显示为
'MySql.Data.MySqlClient.MySqlException:重复字段'
即使它不是主键。我也使用代码检查datagridview中的重复项。
If DataGridView1.RowCount = 0 Then
Dim x As DialogResult
x = MessageBox.Show("Enter the Values", "TDP Thermoline",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim bolDuplicateWasFound As Boolean = False
For x As Integer = 0 To DataGridView1.Rows.Count - 1
For y As Integer = 0 To DataGridView1.Rows.Count - 1
If y <> x AndAlso DataGridView1.Rows(x).Cells("Date").Value = DataGridView1.Rows(y).Cells("Date").Value.ToString Then
MessageBox.Show("Duplicate Found" & DataGridView1.Rows(x).Cells("Date").Value.ToString)
Exit Sub
End If
Next
Next
If Not bolDuplicateWasFound Then
Dim command As MySqlCommand
For i As Integer = 0 To DataGridView1.Rows.Count - 1 Step +1
command = New MySqlCommand("INSERT INTO `daily_production`(`Year`, `Month`, `Date`, `White`, `Yellow`)
VALUES (@yea,@mon,@dat,@whit,@yel)", db.getConnection)
command.Parameters.Add("@yea", MySqlDbType.VarChar).Value = DataGridView1.Rows(i).Cells(0).Value.ToString
command.Parameters.Add("@mon", MySqlDbType.VarChar).Value = DataGridView1.Rows(i).Cells(1).Value.ToString
command.Parameters.Add("@dat", MySqlDbType.VarChar).Value = DataGridView1.Rows(i).Cells(2).Value.ToString
command.Parameters.Add("@whit", MySqlDbType.VarChar).Value = DataGridView1.Rows(i).Cells(3).Value.ToString
command.Parameters.Add("@yel", MySqlDbType.VarChar).Value = DataGridView1.Rows(i).Cells(4).Value.ToString
db.getConnection.Open()
command.ExecuteNonQuery()
Next
MessageBox.Show("Data Insert", "TDP Thermoline", MessageBoxButtons.OK, MessageBoxIcon.Information)
db.getConnection.Close()
db.getConnection.Dispose()
End If
End If
如何清除该错误?