C # Analysis of reading mdb by column for Access operations

Source: Internet
Author: User
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:

 
 
  1. using System.Data.OleDb;
  2. using System.Data;

C # operation Access connection string:

 
 
  1. String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
  2. Data Source=product.mdb";

C # create a connection using Access:

 
 
  1. OleDbConnection connection = new OleDbConnection(connectionString);

C # Use the OleDbCommand class for operation Access to execute SQL statements:

 
 
  1. OleDbCommand cmd = new OleDbCommand(sql, connection);
  2. connection.Open();
  3. 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)

 
 
  1. // Read mdb data
  2. Public static DataTable ReadDataByColumns (string mdbPaht,
  3. String tableName, string [] columns, ref bool success)
  4. {
  5. DataTable dt = new DataTable ();
  6. Try
  7. {
  8. DataRow dr;
  9. // 1. Establish a connection C # Read mdb by column for Access operations
  10. String strConn = @ "Provider = Microsoft.
  11. Jet. OLEDB.4.0; Data Source ="
  12. + MdbPath + "; Jet OLEDB: Database Password = haoren ";
  13. OleDbConnection odcConnection =
  14. New OleDbConnection (strConn );
  15. // 2. Open the connection C # Read mdb by column for Access operations
  16. OdcConnection. Open ();
  17. // Create an SQL query
  18. OleDbCommand odCommand = odcConnection. CreateCommand ();
  19. // 3. Enter the query statement
  20. String strColumn = "";
  21. For (int I = 0; I <columns. Length; I ++)
  22. {
  23. StrColumn + = columns [I]. ToString () + ",";
  24. }
  25. StrColumn = strColumn. TrimEnd (',');
  26. OdCommand. CommandText = "select" + strColumn +
  27. "From" + tableName;
  28. // Create a read C # Read mdb by column for Access operations
  29. OleDbDataReader odrReader =
  30. OdCommand. ExecuteReader ();
  31. // Query and display data C # Read mdb by column for Access operations
  32. Int size = odrReader. FieldCount;
  33. For (int I = 0; I <size; I ++)
  34. {
  35. DataColumn dc;
  36. Dc = new DataColumn (odrReader. GetName (I ));
  37. Dt. Columns. Add (dc );
  38. }
  39. While (odrReader. Read ())
  40. {
  41. Dr = dt. NewRow ();
  42. For (int I = 0; I <size; I ++)
  43. {
  44. Dr [odrReader. GetName (I)] = odrReader [
  45. OdrReader. GetName (I)]. ToString ();
  46. }
  47. Dt. Rows. Add (dr );
  48. }
  49. // Close connection C # Read mdb by column for Access operations
  50. OdrReader. Close ();
  51. OdcConnection. Close ();
  52. Success = true;
  53. Return dt;
  54. }
  55. Catch
  56. {
  57. Success = false;
  58. Return dt;
  59. }
  60. }

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.

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.