The Access database connection uses OleDbConnection object code 1 PrivateFunctiongetAccessConnection () AsOleDbConnectionDimdbConnectionStringAsStringProviderMicrosoft. Jet. OLEDB.4.0; DataSource | DataDirectory | Resourcesmag. mdb; PersistSecurit
The Access database connection adopts OleDbConnection object code 1 Private Function getAccessConnection () As OleDbConnection Dim dbConnectionString As String = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = | DataDirectory | \ Resources \ mag. mdb; Persist Securit
AccessDatabaseConnection
OleDbConnection object
Code 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
Code 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) & "accessDatabaseConnectionFailed "& Chr (13)," warning ")
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:
AccessDatabaseFile, you can use My Project-> resources-> Add existing files to add
In the debugging statusDatabaseWill overwriteDatabase
Author: