熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.NET連接Access數據庫

2022-06-13   來源: .NET編程 

  Access數據庫連接
    采用OleDbConnection對象
    代碼

  

  Private Function getAccessConnection() As OleDbConnection

  Dim dbConnectionString As String = Provider=MicrosoftJetOLEDB;

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

  Try
              dbConnectionOpen()
            Catch Ex As Exception
              MsgBox(ErrDescription)
            End Try

  Return dbConnection

  End Function

  代碼

     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 dbConnectionStateToString = Closed Then
              MsgBox(Chr() & access 數據庫連接失敗 & Chr() 警告)
              Exit Sub
            End If

  DataAdapterSelectCommand = New OleDbCommand(sqlStrdbConnection)

  Try
              DataAdapterFill(dataSetTable)
              DataGridViewDataSource = dataSetTables(Table)DefaultView
            Catch Ex As Exception
              MsgBox(ErrDescription)

  Finally
              dataSetDispose
              DataAdapterDispose
              dbConnectionClose
              dbConnectionDispose
            End Try

  End Sub

  PS
    Access數據庫文件可以通過 My Project > 資源 > 添加資源 > 添加現有文件 這樣的方式來添加
    調試狀態下每次運行項目Resources下面的數據庫將會覆蓋Debug下的數據庫


From:http://tw.wingwit.com/Article/program/net/201311/12786.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.