How to use C # To process ACCESS databases

Source: Internet
Author: User
The code for using the C # link to ACCESS is as follows: usingSystem. data; usingSystem. data. oleDb; OleDbConnectionconnectionnewOleDbConnection (ProviderMicrosoft. jet. OLEDB.4.0; + performancec: DbViewtysql. mdb); open the database connection. open (); MessageB

The code for using C # To link to ACCESS is as follows: using System. data; using System. data. oleDb; OleDbConnectionconnection = new OleDbConnection (Provider = Microsoft. jet. OLEDB.4.0; + DataSource = C: \ DbView \ tysql. mdb); // open the database connection. open (); MessageB

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 ");
// OpenDatabaseConnection
Connection. Open ();
MessageBox. Show ("OpenDatabaseConnection successful ");

// CloseDatabaseConnection
Connection. Close ();
MessageBox. Show ("DisabledDatabaseConnection successful ");

AccessDatabaseAn additional namespace needs to be imported. 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 relies on this and Access.DatabaseConnected.

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

Ps:
1. If you want to connectDatabaseThe file and the current file are in the same directory and can be connected using the following method:
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.DatabaseWe have to deal with this object.

"ObjConnection. Open ();" this is used to Open the connection. At this pointDatabaseThe connection is complete. For other operations (insert, delete...), see related books.
C # query ACCESSDatabase:

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 (); // OpenDatabaseConnection
9 OleDbCommand command = new OleDbCommand (SQL, connection );
10 // Execute SQL query
11 num = <

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.