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下的数据库