VB. NET connection to Access database

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.