访问Access数据库时获取异常

时间:2013-05-16 12:59:40

标签: database vb.net ms-access

我需要在Access表中插入一行。我一直在

  

对象引用未设置为对象的实例

我的代码是:

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim strconstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Daisy\My Documents\Downloads\MusicSales.mdb"
    Dim objcon As OleDb.OleDbConnection
    objcon = New OleDb.OleDbConnection(strconstring)
    Dim objcommand As OleDb.OleDbCommand
    Dim da As New OleDb.OleDbDataAdapter
    Try
        objcon.Open()
        Dim command As String
        command = "insert into Artists(Artist, Company, Sales )" _
             & " values('" & ArtistBox.Text & "', '" _
             & TextBox2.Text & "', " & TextBox3.Text & ")"
        objcommand = New OleDb.OleDbCommand(command, objcon)
        da.InsertCommand.CommandText = command
        da.InsertCommand.ExecuteNonQuery()
    Catch exceptionobject As Exception
        MessageBox.Show(exceptionobject.Message)
    Finally
        objcon.Close()
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

您的连接字符串有点乱,因此可能导致问题。使用EITHER ......

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Daisy\My Documents\Downloads\MusicSales.mdb;

... ... OR

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Daisy\My Documents\Downloads\MusicSales.mdb;