与Access数据库建立连接时出现问题 [英] Problem establishing connection with Access Database

查看:68
本文介绍了与Access数据库建立连接时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新模块,并使用了以下代码:

I created a new module and used the below codes:

Imports System.Data
Imports System.Data.OleDb
Module myConnectionModule
    Public myData As DataGridView
    Public MyConnection As OleDbConnection
    Public MyAdapter As OleDbDataAdapter
    Public MyReader As OleDbDataReader
    Public myConnectionString As String
    Public Sub OpenConnection()
        
        MyConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = dbSuperStripe.accdb")

        MyConnection.Open()
        MyAdapter = New OleDbDataAdapter(myConnectionString, MyConnection)
        MyReader = MyAdapter.SelectCommand.ExecuteReader()

    End Sub

    Public Sub CloseConnection()
        MyConnection.Close()
        MyConnection.Dispose()
        MyAdapter.Dispose()
        MyReader.Close()
    End Sub
End Module

然后,我在Super Stripe \ bin \ Debug中创建了MS Access数据库(dbSuperStripe.accdb).但是它没有与数据库连接.我做错什么了吗?我该怎么办?

Then I created the MS Access Database (dbSuperStripe.accdb) in Super Stripe\bin\Debug. But it is not connected with the database. Am I doing anything wrong? What should I do?

Faisal

推荐答案

Hi Faisal2638,

Hi Faisal2638,

感谢您在MSDN论坛中发帖.

Thank you for posting in MSDN forum.

通过调试找到了一些错误消息吗?在哪一行发生错误?

Have you find some error message by debugging? And in which line you occur error?

我认为,也许未为命令对象设置命令文本.如果您的问题是这样,则可以参考以下代码:

In my opinion, maybe the command text was not set for the command object. If your issue is that, you could refer the following code:

Public myData As DataGridView
    Public MyConnection As OleDbConnection
    Public MyAdapter As OleDbDataAdapter
    Public MyReader As OleDbDataReader
    Public myConnectionString As String
    Public cmd As OleDbCommand

    Public Sub OpenConnection()
        MyConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = Database1.accdb")

        MyConnection.Open()
        MyAdapter = New OleDbDataAdapter(myConnectionString, MyConnection)
        cmd = New OleDbCommand("select * from Table1", MyConnection)
        MyReader = cmd.ExecuteReader()

    End Sub

如果要连接Access数据库,并在DataGridView中显示数据.希望以下代码段对您有所帮助:

And if you want to connection Access database, and show the data in DataGridView. Hope the following code snippet will be helpful to you:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=YourPath\Database1.accdb;Persist Security Info = False;"
        Dim Conn As OleDbConnection = New OleDbConnection(ConnStr)
        Dim com As OleDbCommand = New OleDbCommand("SELECT * FROM Table1", Conn)
        Conn.Open()
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(com)
        Dim ds As DataSet = New DataSet()
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
    End Sub

有关Access连接字符串的更多信息,您可以参考以下链接: https://www.connectionstrings.com/access/

For more information about Access connection string you could refer this link: https://www.connectionstrings.com/access/

最好的问候,

Neda Zhang

Neda Zhang


这篇关于与Access数据库建立连接时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆