Connecting to an ACCESS DATABASE

hys

Member
Joined
Apr 10, 2003
Messages
7
Hello guys,

Just a quick question...

Ive spent hours reading tech. guides but had no success..

How do i connect to an access database (data.mdb) and run the query:

Code:
"SELECT members.UserName FROM members WHERE (((members.UserName)=" & User & ") AND ((members.Password)=" & Password & "))"

ignore the User and Password those are variables which will be passed through in the code

then i need to get the UserName that returns in the query ...

and a recordcount that comes back ..
 
You can do it through the following

Add an Imports System.Data.Oledb to your class

Dim connection as new oledbconnection()
connection.connectionstring = "Supply the connection to your db"

Your connection might be :
Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\product" & _
"s.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";J" & _
"et OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5" & _
";Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB" & _
":Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create " & _
"System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Dont Copy Loca" & _
"le on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP" & _
"=False"
Dont panic just supply in the data source field the path to your Access db file..

Dim command as new oledbcommand()
command.connection = connection
command.commandtype = commandtype.text
command.commandtext = "Your SQL Query here"

finally execute the command, you may have two scenarios
If you are inserting or updating to the database
you may not need to return anything so type

command.ExecuteNonQuery()

if you are doing a select statement then do the following
declare a reader to hold your data
Dim reader as OledbDataReader
set the reader to the result of your query
reader = command.ExecuteReader()
now the reader has the data returned

finally close the reader and the connection

reader.close()
connection.close()
 
Back
Top