hi,
how could i can connect C# with ms access 2007 as i want to use the data in a form of table in my C# program.As im creating a system to input the marks of the student and the list of student is in ms access so need to input that list in C#.

Recommended Answers

All 3 Replies

Hi there,

In order to connect to the MS Access 2007 you should use this snippet of code:

string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\resources\pw.accdb;
Jet OLEDB:Database Password=password";

OleDbConnection DBCon = new OleDbConnection(ConnStr);

When you have established a connection with the db, all you have to do is to create a DataAdapter Object in order to run the query you want, and then create a Dataset Object to store the information that you got from the db.

(In order to fill the Dataset object, use the .Fill() function of the DataAdapter Object)

Hope i helped :)

oleDbConnExcel = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=;Extended properties=Excel 12.0;Data Source=" + System.IO.Directory.GetCurrentDirectory() + "\\FF_Sales_082zhengda(1).xlsx");
// oleDbConnExcel = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source=E:\\FF_Sales_082zhengda(1).xls");
oleDbConnExcel.Open();
strGetDataFromExcel = "SELECT * FROM [Sheet1$A11:G]"; //从有效行开始取值
oleDbCmdExcel = new OleDbCommand(strGetDataFromExcel, oleDbConnExcel);
oleDbDataReaderExcel = oleDbCmdExcel.ExecuteReader();

try
            {

                //oleDbConnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.IO.Directory.GetCurrentDirectory() + "\\ff.mdb");
                //oleDbConnAccess = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\test.mdb");
                oleDbConnAccess = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.IO.Directory.GetCurrentDirectory() + "\\ff.accdb");
                
                oleDbConnAccess.Open();
                strDeleteAccess = "delete from ff_sales_082 ";
                oleDbCmdAccess = new OleDbCommand(strDeleteAccess, oleDbConnAccess);
                oleDbCmdAccess.ExecuteNonQuery();
                oleDbCmdAccess.Dispose();
                oleDbConnAccess.Close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.