Reputation: 1403
i want to know how to connect an Access 2007 database and retrieve data from that using C#.net2005. I don't know anything about database connectivity.Please help me i'm new in this.If u are busy to explain can u mention any good link to the tutorial for that
Upvotes: 0
Views: 577
Reputation: 16903
If you want to connect to Access Database.
You have to follow the following steps:-
1) First add "using System.Data.OleDb;" at to top of the CS file.
2) Create connection string and open connection.
For access 2007
string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;Jet OLEDB:Database Password=password";
OleDbConnection MyConn = new OleDbConnection(ConnStr);
3) Open this connection.
MyConn.Open();
4) Create object for command and reader to get the data from access database.
OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn);;
OleDbDataReader ObjReader = Cmd.ExecuteReader();
5) Now look through the reader object to get the data
if (ObjReader != null)
{
}
6) After completing the processing
ObjReader.Close();
MyConn.Close();
Upvotes: 1
Reputation: 4711
C# How to connect to MS Access 2007 - you can find code example there.
Also you do the following.
Create a new winforms application.
Go to Server Explorer (View->Server Explorer, Ctrl + Alt + S)
There on server explorer, right click on data connections and add a data connection.
Choose the Add Connection option.
There change the Data source to Access Database.
Open the mdb file you want to attach. It will generate its connection string automatically.
Then you get to choose the tables you want to insert and the query to use.
Just select the table and click on all fields.
Once the connection is done, drag it and drop on the form. Visual Studio will generate the code behind for you automatically.
Upvotes: 1
Reputation: 38365
Use an OleDbConnection, and learn ADO.NET: http://msdn.microsoft.com/en-us/data/aa937699.aspx
Upvotes: 1