C # How does one implement reading mdb by column for Access operations? First, let's take a look at the basic knowledge: This article C # basic knowledge of operating the Access database, and provide a related instance. C # ADO. NET cannot create a new ACCESS (MDB) database by programming, so it can only use ADOX as the link library from COM for operations. C # Access
C # How does one implement reading mdb by column for Access operations? First, let's take a look at the basic knowledge: This article C # basic knowledge of operating the Access database, and provide a related instance. C # ADO. NET cannot create a new ACCESS (MDB) database by programming, so it can only use ADOX as the link library from COM for operations. C # Access
C # How does one implement reading mdb by column for Access operations? First, let's take a look at the basic knowledge: This article C # basic knowledge of operating the Access database, and provide a related instance. C # ADO. NET cannot create a new ACCESS (MDB) database by programming, so it can only use ADOX as the link library from COM for operations.
C # The main knowledge points of Access operations are as follows:
- using System.Data.OleDb;
-
- using System.Data;
C # operation Access connection string:
- String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
-
- Data Source=product.mdb";
C # create a connection using Access:
- OleDbConnection connection = new OleDbConnection(connectionString);
C # Use the OleDbCommand class for operation Access to execute SQL statements:
- OleDbCommand cmd = new OleDbCommand(sql, connection);
-
- connection.Open();
-
- cmd.ExecuteNonQuery();
C # example of reading mdb content by column for Access operations:
Columns array stores the name of the column you want to query (ensure that the column you want exists in the mdb table)
- // Read mdb data
- Public static DataTable ReadDataByColumns (string mdbPaht,
- String tableName, string [] columns, ref bool success)
- {
- DataTable dt = new DataTable ();
- Try
- {
- DataRow dr;
- // 1. Establish a connection C # Read mdb by column for Access operations
- String strConn = @ "Provider = Microsoft.
- Jet. OLEDB.4.0; Data Source ="
- + MdbPath + "; Jet OLEDB: Database Password = haoren ";
- OleDbConnection odcConnection =
- New OleDbConnection (strConn );
- // 2. Open the connection C # Read mdb by column for Access operations
- OdcConnection. Open ();
- // Create an SQL query
- OleDbCommand odCommand = odcConnection. CreateCommand ();
- // 3. Enter the query statement
- String strColumn = "";
- For (int I = 0; I <columns. Length; I ++)
- {
- StrColumn + = columns [I]. ToString () + ",";
- }
- StrColumn = strColumn. TrimEnd (',');
- OdCommand. CommandText = "select" + strColumn +
- "From" + tableName;
- // Create a read C # Read mdb by column for Access operations
- OleDbDataReader odrReader =
- OdCommand. ExecuteReader ();
- // Query and display data C # Read mdb by column for Access operations
- Int size = odrReader. FieldCount;
- For (int I = 0; I <size; I ++)
- {
- DataColumn dc;
- Dc = new DataColumn (odrReader. GetName (I ));
- Dt. Columns. Add (dc );
- }
-
- While (odrReader. Read ())
- {
- Dr = dt. NewRow ();
- For (int I = 0; I <size; I ++)
- {
- Dr [odrReader. GetName (I)] = odrReader [
- OdrReader. GetName (I)]. ToString ();
- }
- Dt. Rows. Add (dr );
- }
- // Close connection C # Read mdb by column for Access operations
- OdrReader. Close ();
- OdcConnection. Close ();
- Success = true;
- Return dt;
- }
- Catch
- {
- Success = false;
- Return dt;
- }
- }
C # the basic content of reading mdb by column for Access operations is introduced here. I hope to help you understand and learn how to read mdb by column for Access Operations C.