VB.net错误:cmd.ExecuteNonQuery

 收藏

嗨,大家好,

我在cmd.ExecuteNonQuery()面临问题 请纠正我,那是错误吗? n如何纠正?

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim sqlinsert As String
        ' We use the INSERT statement which tells our program to add the information
        ' from the Forms Text fields into the Databases columns.

        sqlinsert = "INSERT INTO Contacts(FirstName, LastName, Address, CompanyName, CompanyAddress, PersonalAC, CompanyAC, PhoneNumber, Remarks, IDNum)" & _
      "VALUES(@FirstName, @LastName, @Address, @CompanyName, @CompanyAddress, @PersonalAC, @CompanyAC, @PhoneNumber, @Remarks, @IDNum)"
        Dim cmd As New OleDbCommand(sqlinsert, con1)
        ' This assigns the values for our columns in the DataBase.
        ' To ensure the correct values are written to the correct column
        cmd.Parameters.Add(New OleDbParameter("@FirstName", txtFirst.Text))
        cmd.Parameters.Add(New OleDbParameter("@LastName", txtLast.Text))
        cmd.Parameters.Add(New OleDbParameter("@Address", txtAddress.Text))
        cmd.Parameters.Add(New OleDbParameter("@CompanyName", txtCompanyN.Text))
        cmd.Parameters.Add(New OleDbParameter("@CompanyAddress", txtCompanyA.Text))
        cmd.Parameters.Add(New OleDbParameter("@PersonalAC", txtPersonalAC.Text))
        cmd.Parameters.Add(New OleDbParameter("@CompanyAC", txtCompanyAC.Text))
        cmd.Parameters.Add(New OleDbParameter("@PhoneNumber", txtPhone.Text))
        cmd.Parameters.Add(New OleDbParameter("@Remarks", txtRemarks.Text))
        cmd.Parameters.Add(New OleDbParameter("@IDNum", txtID.Text))
        ' This is what actually writes our changes to the DataBase.
        ' You have to open the connection, execute the commands and
        ' then close connection.
        con1.Open()
        cmd.ExecuteNonQuery() ' ERROR AT HERE
        con1.Close()
        ' This are subs in Module1, to clear all the TextBoxes on the form
        ' and refresh the DataGridView on the MainForm to show our new records.
        ClearTextBox(Me)
        RefreshDGV()
        Me.Close()
    End Sub
回复
  • qporro 回复

    首先,这是一个非常古老的线程。您应该打开一个新的。 第二,我看到您关闭Conn,但再也没有打开它。尝试在将其分配给命令之前或之后将其打开。 如果这不能解决问题,请打开一个新线程,再次发布代码,让我们知道您遇到的具体错误。

    祝好运

  • 9527 回复

    亲爱的rponraj,请为您的问题打开一个新线程。不要在其他人发表。

  • znon 回复

    我在cmd.ExecuteNonQuery()处收到以下错误

    用户未处理SqlException

  • 克制 回复

    尝试

    cmd.parameters.addwithvalue("@param",value)
    

    可能对您有帮助,但是最好告诉我们您在cmd.executenonquery()遇到什么错误

    问候

  • 米唐 回复

    使用Try..Catch块并捕获错误。

    Try
        ' the procedure
    Catch Ex as Exception
        MsgBox (ex.toString)
    End Try
    

    您也可以尝试打印出参数。

    For Each p As OleDb.OleDbParameter In cmd.Parameters
        Console.WriteLine("Name:{0} Value{1}", p.ParameterName, p.Value.ToString)
    Next
    

    确保参数为空,为空或为空时,必须将其设置为DBNull。

    cmd.Parameters.Add(New OleDbParameter("@FirstName", IIf(txtFirst.Text = "", DBNull.Value, txtFirst.Text)))
    

  • 冉天心 回复

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If TextBox1.Text = "" Then
                MsgBox("Nomor Faktur Belum di isi !! ", MsgBoxStyle.Exclamation,
                "Peringatan")
            Else
                Dim simpan1 As String
                CMD = Conn.CreateCommand
                Call bukaDB()
                simpan = "INSERT INTO tbbeli (nofak,tanggal,kodesup,totalitem,totalbayar)VALUES (@p1,@p2,@p3,@p4,@p5)"
                simpan1 = "INSERT INTO tbdetailbeli (nofak,kodebarang,jumlah) VALUES(@p6,@p7,@p8) "
                ubah = "UPDATE tbbarang SET stok=stok+@p9 WHERE kodebarang = @p10"
                With CMD
                    .CommandText = simpan
                    .Connection = Conn
                    .Parameters.AddWithValue("p1", MySqlDbType.String).Value = TextBox1.Text
                    .Parameters.AddWithValue("p2", MySqlDbType.DateTime).Value = Format(Now, "dd-MM-yyyy")
                    .Parameters.AddWithValue("p3", MySqlDbType.String).Value = ComboBox1.Text
                    .Parameters.AddWithValue("p4", MySqlDbType.Int32).Value = Label9.Text
                    .Parameters.AddWithValue("p5", MySqlDbType.Int32).Value = Label10.Text
                    .ExecuteNonQuery()   "(ERROR IN HERE)"
                    Conn.Close()
                End With
                For i As Integer = 0 To DataGridView1.Rows.Count - 2
                    CMD = Conn.CreateCommand
                    With CMD
                        .CommandText = simpan
                        .Connection = Conn
                        .Parameters.AddWithValue("p6", MySqlDbType.String).Value = TextBox1.Text
                        .Parameters.AddWithValue("p7", MySqlDbType.String).Value = DataGridView1.Rows(i).Cells(0).Value
                        .Parameters.AddWithValue("p8", MySqlDbType.Int32).Value = DataGridView1.Rows(i).Cells(3).Value
                        .ExecuteNonQuery()        "(ERROR IN HERE)"
                        Conn.Close()
                    End With
                    CMD = Conn.CreateCommand
                    With CMD
                        .CommandText = ubah
                        .Connection = Conn
                        .Parameters.AddWithValue("p9", MySqlDbType.UInt32).Value = DataGridView1.Rows(i).Cells(3).Value
                        .Parameters.AddWithValue("p10", MySqlDbType.String).Value = DataGridView1.Rows(i).Cells(0).Value
                        .ExecuteNonQuery()  "(ERROR IN HERE)"
                        Conn.Close()
                    End With
                Next
            End If
            Conn.Close()
            CMD.Dispose()
            bersih()
        End Sub

  • 小不点er 回复

    是一个旧线程,不是吗?

  • nsit 回复

    我遇到了麻烦...将输入交易购买数据中的数据更改存储到tbbarang和tbsuplier

  • 我 也呸! 回复

    我相信您尚未开始与数据库建立连接

    请查看下面的代码,然后尝试连接数据库

    Public Sub CreateCommand(ByVal queryString As String,_ ByVal connectionString As String) 将连接用作新的SqlConnection(connectionString) 昏暗的命令作为新的SqlCommand(queryString,connection) command.Connection.Open() command.ExecuteNonQuery() 最终使用 结束子