尝试并在插入MySQL中捕获MysqlCmd.ExecuteNonQuery

时间:2015-09-24 05:12:32

标签: mysql database visual-studio visual-studio-2010 mysql-error-1064

当我插入null时,它在我的代码中显示错误。我可以尝试抓住它吗?我不知道怎么做。

Call Connects()
MySqlCmd = New MySqlCommand
MySqlCmd.Connection = Myconnect
MySqlCmd.CommandText = "Select ID,ItemName,Quantity,ItemDesc,SuppInfo,datee from item "
MyDA = New MySqlDataAdapter
myDataTable = New DataTable
MyDA.SelectCommand = MySqlCmd
MyDA.Fill(myDataTable)
MySqlCmd = New MySqlCommand
MySqlCmd.Connection = Myconnect
MySqlCmd.CommandText = "INSERT INTO item(ID,ItemName,Quantity,ItemDesc,SuppInfo,Po_Number,datee) VALUES('" & TextBox7.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "',CURRENT_TIMESTAMP)"
MySqlCmd.ExecuteNonQuery()
MsgBox("New working", MsgBoxStyle.Information)

我能做些什么吗?

以下是显示错误的屏幕截图:

2 个答案:

答案 0 :(得分:0)

检查TextBox3.Text的值,如果无法将其解析为int,则将其替换为0.如果要插入NULL,请精确指定,而不是使用空字符串。

答案 1 :(得分:0)

  If Label30.Text = 1 Then
            Try
                If TextBox7.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Then
                    MessageBox.Show("ERROR!")
                ElseIf TextBox7.TextLength > 0 And TextBox2.TextLength > 0 And TextBox3.TextLength > 0 And TextBox4.TextLength > 0 And TextBox5.TextLength > 0 And TextBox6.TextLength > 0 Then
                    Call Connects()
                    MySqlCmd = New MySqlCommand
                    MySqlCmd.Connection = Myconnect
                    MySqlCmd.CommandText = "Select ID,ItemName,Quantity,ItemDesc,SuppInfo,datee from item "
                    MyDA = New MySqlDataAdapter
                    myDataTable = New DataTable
                    MyDA.SelectCommand = MySqlCmd
                    MyDA.Fill(myDataTable)

它的工作现在谢谢你:)这就是我解决自己问题的方法。