【问题标题】:Try and catch in MysqlCmd.ExecuteNonQuery in insert MySQL尝试在插入 MySQL 的 MysqlCmd.ExecuteNonQuery 中捕获
【发布时间】:2015-12-21 14:41:40
【问题描述】:

当我插入一个空值时,它会在我的代码中显示一个错误。我可以试着抓住它吗?我不知道怎么做。

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)

有什么我可以做的吗?

这是显示错误的屏幕截图:

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
        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)
      

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

      【讨论】:

        猜你喜欢
        • 2011-03-25
        • 1970-01-01
        • 1970-01-01
        • 2018-03-11
        • 2015-09-07
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多