How to use C # To process ACCESS databases

Source: Internet
Author: User

The code for using the C # link to ACCESS is as follows:
Using System. Data;
Using System. Data. OleDb;


OleDbConnection connection = new OleDbConnection ("Provider = Microsoft. Jet. OLEDB.4.0;" +
"Data Source = C: \ DbView \ tysql. mdb ");
// Open the database connection
Connection. Open ();
MessageBox. Show ("successful database connection ");

// Close the database connection
Connection. Close ();
MessageBox. Show ("the database connection is closed successfully ");

To connect to the Access database, you need to import additional namespaces. Therefore, the first two using commands are required!

"Provider = Microsoft. jet. oleDb.4.0; "is the index data provider. Here we use the Microsoft Jet Engine, which is the Data Engine in Access. asp.net is connected to the Access database.

"Data Source = C: BegASPNETNorthwind. mdb" indicates the location of the Data Source. Its standard format is "Data Source = MyDrive: MyPathMyFile. MDB ".

Ps:
1. If the database file to be connected is in the same directory as the current file, you can use the following method to connect:
StrConnection + = "Data Source = ";
StrConnection + = MapPath ("Northwind. mdb ");
In this way, you can write a lot of things!
3. Note that parameters in the connection string must be separated by semicolons.

"OleDbConnection objConnection = new OleDbConnection (strConnection);" this sentence uses the defined connection string to create a link object. In the future, we will deal with this object for database operations.

"ObjConnection. Open ();" this is used to Open the connection. Now, the connection to the Access database is complete. For other operations (insert, delete...), see related books.
C # query the ACCESS database:

1 int num = 0; // Number of Members
2 string message = ""; // the pop-up result message.
3 // SQL statement used for query
4 string SQL = "SELECT COUNT (*) FROM Orders ";
5
6 try
7 {
8 connection. Open (); // Open the database connection
9 OleDbCommand command = new OleDbCommand (SQL, connection );
10 // Execute SQL query
11 num = <

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.