通过VB访问数据库

时间:2014-05-02 11:27:49

标签: vb.net

我通过vb adodb连接访问数据库。 我添加了

Module Module1
    Public con As New ADODB.Connection
    Public rs As New ADODB.Recordset
    Public Sub connection()
        con = New ADODB.Connection
        con.Open("Database1.mdb")
    End Sub
    Public Sub opentable(ByRef str As String)
        rs = con.Execute(str)
    End Sub
End Module

Public Class Form1
    Dim first As String
    Dim second As String
    Dim str As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connection()

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        opentable("INSERT INTO [User] (FirstName , SurName) VALUES('" & first & "','" & second & "')")
    End Sub
End Class

问题是,当我访问时,我收到错误

  

WindowsApplication1.exe中出现未处理的“System.Runtime.InteropServices.COMException”类型异常

     

附加信息:关闭对象时不允许操作。

1 个答案:

答案 0 :(得分:0)

我认为你在VB.Net中使用VB 6或COM,

将您的连接字符串更改为此类

con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=fullpath\Database1.mdb;")