C # connect to Access
Using System. Data;
Using System. Data. OleDb;
String strConnection = "Provider = Microsoft. Jet. OleDb.4.0 ;";
StrConnection = @ "Data Source = C: BegASPNETNorthwind. mdb ";
OleDbConnection objConnection = new OleDbConnection (strConnection );
ObjConnection. Open ();
ObjConnection. Close ();
Connection details:
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: BegASPNETNorthwind. mdb" indicates the location of the Data Source,
The standard format is "Data Source = MyDrive: MyPathMyFile. MDB ".
PS:
1. the "@" symbol after "=" prevents the "" in the subsequent string from being parsed as an escape character.
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 ();" this is used to Open the connection. At this point, the connection to the Access database is complete.