Connecting to an Access database

by CameronM 6. October 2009 20:16

Class Declarations

Imports System.Data.OleDb

Function Code

'declare the Sql data controls

Dim conn As New OleDbConnection()

Dim cmd As New OleDbCommand

 

'declare and initialise the connection string

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|Users.mdb"

 

'assign the string and open the SQL connection control

conn.ConnectionString = strConnection

conn.Open()

 

'set the command type depending on the query being used

'For dynamically created SQL

cmd.CommandType = CommandType.Text

cmd.CommandText = "SELECT * FROM tablename WHERE FilterBy LIKE @ParamName"

 

 

'set the connection to use with the command

cmd.Connection = conn

 

'utilise the command in one of the methods below...

Tags: , , ,

Access