VB.net how to connect to an Access database

Source: Internet
Author: User
Access database connection
Using the 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 & "Access database connection Failed" & 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:
Access database files, which can be added by adding resources-> the existing files through the My Project-> resource->
Under Debug state, each time the project is run, the database under resources will overwrite the database under debug
For information about how Excel is connected, refer to http://blog.csdn.net/SDLin/archive/2008/02/29/2133183.aspx (how to read Excel files and assign values to DataGridView objects)
about how the database operates, reference http://blog.csdn.net/SDLin/archive/2008/02/29/2133016.aspx (how the database synchronizes with the DataTable)



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.