1. 程式人生 > 其它 >vb.net 如何連線Access資料庫

vb.net 如何連線Access資料庫

技術標籤:vb.net教程vb.net教程

Access資料庫vb.net教程
連線
採用OleDbConnection對c#教程

程式碼 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 -> 資源 -> 新增資源 -> 新增python基礎教程現有檔案 這樣的方式來新增
除錯狀態下,每次執行專案,Resources下面的資料庫將會覆蓋Debug下的資料庫

本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連結和本宣告。
本文連結:https://blog.csdn.net/sdlin/article/details/2192347