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 ...)