Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read mdb by column when operating Access in C #

Shulou Source: shulou.com Published: 2022-06-02 09:29:17 10月10日 Update

Editor to share with you how to operate Access in C# to read mdb by column. I hope you will get something after reading this article. Let's discuss it together.

ADO.NET in C # cannot programmatically create a completely new ACCESS (MDB) database, so it can only be operated using ADOX, a link library from COM.

The main knowledge points of Access operation in C # are as follows:

Using System.Data.OleDb; using System.Data

C # operates the Access connection string:

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

C # operate Access to establish a connection:

OleDbConnection connection = new OleDbConnection (connectionString)

The C # operation Access uses the OleDbCommand class to execute the Sql statement:

OleDbCommand cmd = new OleDbCommand (sql, connection); connection.Open (); cmd.ExecuteNonQuery ()

An example of reading mdb content by column in C# operation Access is as follows:

The columns array stores the name of the column you want to query (you must make sure 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, read mdb 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# operation Access to read mdb odcConnection.Open () by column; / / establish the SQL query OleDbCommand odCommand = odcConnection.CreateCommand (); / / 3, enter the query statement string strColumn = ""; for (int I = 0; I < columns.Length; iTunes +) {strColumn+ = columns [I] .ToString () + ",";} strColumn = strColumn.TrimEnd (','); odCommand.CommandText = "select" + strColumn+ "from" + tableName / / establish column read mdb OleDbDataReader odrReader for read C# operation Access = odCommand.ExecuteReader (); / / query and display data C# operation Access column read mdb int size = odrReader.FieldCount; for (int I = 0; I < size; iTunes +) {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 +) {drr [odrReader.GetName (I)] = odrReader [odrReader.GetName (I)] .ToString ();} dt.Rows.Add (dr);} / close connection C# operation Access read mdb odrReader.Close () by column; odcConnection.Close (); success = true Return dt;} catch {success = false; return dt;}} after reading this article, I believe you have a certain understanding of "how to read mdb by column when operating Access in C#". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

Tags: Caching query data knowledge articles sentences New content name character string done instance Database Array method more knowledge Point Industry Information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Microsoft MySQL Docker