hello dear ! how to make a connection with MS acess database???????????
how we can make a coneection with access database nad how to access tabls and data in c#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Jul 5, 2010, 12:52 AM
using System;
using System.Data.OleDb;
class OleDbTest{
public static void Main()
{
//create the database connection
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\db1.mdb");
//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("select * from emp_test", aConnection);
try
{
aConnection.Open();
//create the datareader object to connect to table
OleDbDataReader aReader = aCommand.ExecuteReader();
Console.WriteLine("This is the returned data from emp_test table");
//Iterate throuth the database
while(aReader.Read())
{
Console.WriteLine(aReader.GetInt32(0).ToString());
}
//close the reader
aReader.Close();
//close the connection Its important.
//Some usual exception handlingaConnection.Close();
}
catch(OleDbException e)
{
Console.WriteLine("Error: {0}", e.Errors[0].Message);
}
}
}
Thanks
accept is as acnswer if its helps you
Muhammad UsmanPosted May 22, 2010, 8:17 AM
CrishPosted May 21, 2010, 1:07 PM
use below line for connecting MSAccess Database.
public static OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & ("MyDB.mdb"));
Syed ShakeerPosted May 21, 2010, 3:19 AM
Hi,
below is code for storing MSAccess connection String in WEB.Config
For MsAccess database we have to wrte Proivder name as "System.Data.OleDb"
<connectionStrings>
<add name="MS-db1ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Documents and Settings\INTHIYAAZPS\My Documents\MSAccess\db1.mdb"" providerName="System.Data.OleDb" />
connectionStrings>
above Data Source will take the Path of your stored MSAccess database..here my MSaccess database stored path is C:\Documents and Settings\INTHIYAAZPS\My Documents\MSAccess\db1.mdb
write the Connection string in Pageload as follows:-
first Import the NameSpace for MSAccess as "Using System.Data.OleDb"
{
OleDbConnection conn_MSACCESS = new OleDbConnection(ConfigurationManager.ConnectionStrings["MS-db1ConnectionString"].ConnectionString);
conn_MSACCESS.Open();
Response.Write("MS_ACCESS_connection Connected sucessfully");
conn_MSACCESS.Close();
}