Connecting to an Access database instance using ADO. NET

Source: Internet
Author: User
Access Program code snippet: usingSystem. Data; usingSystem. Data. OleDb; stringstrConnectionProviderMicrosoft. Jet. OleDb.4.0; strConnection + @ DataSourceC: BegASPNETNorthwind. mdb; OleDbConnectionobjConnectionnewOleDbConnection (strC

Code snippet for connecting to the Access Program: using System. data; using System. data. oleDb; string strConnection = "Provider = Microsoft. jet. oleDb.4.0; "; strConnection + = @" Data Source = C:/BegASPNET/Northwind. mdb "; OleDbConnection objConnection = new OleDbConnection (strC

Code snippet for connecting to the Access Program:

Using System. Data;
Using System. Data. OleDb;

String strConnection = "Provider = Microsoft. Jet. OleDb.4.0 ;";
StrConnection + = @ "Data Source = C:/BegASPNET/Northwind. mdb ";

OleDbConnection objConnection = new OleDbConnection (strConnection );

ObjConnection. Open ();
ObjConnection. Close ();

Explanation:

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

The strConnection variable stores the connection string required to connect to the database. It specifies the data provider to be used and the data source to be used.

"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:/BegASPNET/Northwind. mdb" indicates the location of the Data Source. The standard format is "Data Source = MyDrive: MyPath/MyFile. MDB ".

Note:

1. the "@" symbol after "+ =" prevents "/" in the string from being parsed as escape characters.

2. 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 ();" is used to Open the connection. At this point, the connection to the Access database is complete. We can perform other operations (insert, delete ...)

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.