Rumah  >  Artikel  >  pangkalan data  >  VB.NET连接Access数据库

VB.NET连接Access数据库

WBOY
WBOYasal
2016-06-07 15:17:122087semak imbas

Access 数据库 连接 采用OleDbConnection对象 代码 1 Private Function getAccessConnection() As OleDbConnection Dim dbConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\Resources\mag.mdb;Persist Securit


Access数据库连接
    采用OleDbConnection对象
    代码 1

Private Function getAccessConnection() As OleDbConnection

          Dim dbConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;

    Data Source=|DataDirectory|\Resources\mag.mdb;Persist Security Info=true"
          Dim dbConnection As OleDbConnection = New OleDbConnection(dbConnectionString)

            Try
              dbConnection.Open()
            Catch Ex As Exception
              ’MsgBox(Err.Description)
            End Try

          Return dbConnection

      End Function
 


代码 2

  private Sub fillDataGridView()
          Dim sqlStr As String = "select * from Table"
          Dim DataAdapter As New OleDbDataAdapter
          Dim dataSet As New DataSet
          Dim DataGridView As New DataGridView
          Dim dbConnection As OleDbConnection = getAccessConnection()

            If dbConnection.State.ToString = "Closed" Then
              MsgBox(Chr(13) & " access 数据库连接失败 " & Chr(13), , "警告")
              Exit Sub
            End If

            DataAdapter.SelectCommand = New OleDbCommand(sqlStr,dbConnection)

            Try
              DataAdapter.Fill(dataSet,"Table")
              DataGridView.DataSource = dataSet.Tables("Table").DefaultView
            Catch Ex As Exception
              MsgBox(Err.Description)

      Finally
              dataSet.Dispose
              DataAdapter.Dispose
              dbConnection.Close
              dbConnection.Dispose
            End Try

      End Sub
 


    
    PS:
    Access数据库文件,可以通过 My Project -> 资源 -> 添加资源 -> 添加现有文件 这样的方式来添加
    调试状态下,每次运行项目,Resources下面的数据库将会覆盖Debug下的数据库

 

本文作者:

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:ACCESS数据库之规格为何?Artikel seterusnya:备份还原数据库