无法从VB.NET插入MS Access数据库

时间:2015-09-09 04:16:47

标签: sql vb.net ms-access

VB.NET无法将数据插入MS Access数据库表。

我可以在此代码中修改哪些内容以将数据插入表中?

错误讯息:

  

System.Data.dll中出现未处理的“System.InvalidOperationException”类型异常

     

其他信息:ExecuteNonQuery:尚未初始化Connection属性。

Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click

        Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=loginUser.mdb;")

        Dim dt As New DataTable
        Dim da As New OleDbDataAdapter

        Try
            connection.Open()
            Dim cmd As New OleDbCommand
            cmd.CommandText = "INSERT INTO Staff (ID, username, password) VALUES (@id, @username, @password)"
            cmd.Parameters.AddWithValue("@id", 101)
            cmd.Parameters.AddWithValue("@username", txtUsername.Text)
            cmd.Parameters.AddWithValue("@password", txtPassword.Text)

            cmd.ExecuteNonQuery()
            MsgBox("Add")
        Catch ex As Exception
        Finally
            connection.Close()
        End Try


    End Sub

3 个答案:

答案 0 :(得分:0)

您好,您需要将connection初始化为cmd.Connection属性,将以下代码更改为此代码:

  cmd.Connection = connection
  con.Open()
  cmd.ExecuteNonQuery()

答案 1 :(得分:0)

在创建/执行命令时,您没有提及 Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=loginUser.mdb;") Dim dt As New DataTable Dim da As New OleDbDataAdapter Try Dim cmd As New OleDbCommand("INSERT INTO Staff (ID, username, password) VALUES (@id, @username, @password)",connection) connection.Open() cmd.Parameters.AddWithValue("@id", 101) cmd.Parameters.AddWithValue("@username", txtUsername.Text) cmd.Parameters.AddWithValue("@password", txtPassword.Text) cmd.ExecuteNonQuery() MsgBox("Add") Catch ex As Exception Finally connection.Close() End Try End Sub 。请使用以下代码...

@IBAction func numberList(sender: AnyObject) {
    self.editorView.textStorage?.beginEditing()
    var paragraph : NSMutableParagraphStyle = NSMutableParagraphStyle()
    var textList : NSTextList  = NSTextList(markerFormat: "{decimal}.", options: 0)
    paragraph.textLists = [textList]

    var attributedString : NSAttributedString = NSAttributedString(string: "\t \(textList.markerForItemNumber(1)) \t", attributes: [paragraph : NSParagraphStyleAttributeName])

    self.editorView.textStorage?.appendAttributedString(attributedString)
    self.editorView.textStorage?.endEditing()
}

答案 2 :(得分:0)

异常的原因是您没有将声明的连接分配给命令对象

所以你可以将现有的命令声明修改为

php.ini

Dim cmd As New OleDbCommand("", connection )

我刚刚修改了您的代码:请参阅以下

Dim cmd As New OleDbCommand
cmd.connecction = connection

您可以使用Dim dt As New DataTable Dim da As New OleDbDataAdapter Using OleDbConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=loginUser.mdb;") Dim cmd As New OleDbCommand("", OleDbConn) With cmd OleDbConn.Open() .CommandText = "INSERT INTO Staff (ID, username, password) VALUES (@id, @username, @password)" .Parameters.AddWithValue("@id", 101) .Parameters.AddWithValue("@username", "adfda") .Parameters.AddWithValue("@password", "dfsd") .ExecuteNonQuery() MsgBox("Add") End With End Using 块来处理您的连接,连接将在使用块结束时自动关闭