Sample code Introduction to C # Access database operations
Using system;using system.collections.generic;using system.linq;using system.text;using System.Data;using System.data.oledb;namespace accesspractice{public static class Accesshelper {public static bool Execute (str ing path,string sql) {try {string strconn = @ "Provider=microsoft.jet.oledb. 4.0;data source= "+ path +"; "; OleDbConnection odcconnection = new OleDbConnection (strconn); 2, open the Connection Odcconnection.open (); Build SQL query OleDbCommand Odcommand = Odcconnection.createcommand (); 3, input query statement odcommand.commandtext = SQL; Odcommand.executenonquery (); Odcconnection.close (); return true; } catch (Exception ex) {return false; }} public static DataTable Readalldata (String tableName, String mdbpath,int TopN, ref bool Success) {DataTable dt = new DataTable (); try {//1, establish connection string strconn = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Mdbpath + ";"; OleDbConnection odcconnection = new OleDbConnection (strconn); 2, open the Connection Odcconnection.open (); Build SQL query OleDbCommand Odcommand = Odcconnection.createcommand (); 3, input query statement odcommand.commandtext = "SELECT * from" + tableName; Build Read OleDbDataReader Odrreader = Odcommand.executereader (); querying and displaying data int size = Odrreader.fieldcount; for (int i = 0; i < size; i++) {DataColumn dc; DC = new DataColumn (Odrreader.getname (i)); Dt. Columns.Add (DC); } DataRow Dr; int count = 0; While(Odrreader.read ()) {if (++count = = TopN) {break; } dr = dt. NewRow (); for (int i = 0; i < size; i++) {dr[odrreader.getname (i)] = Odrreader[odrread Er. GetName (i)]. ToString (); } dt. Rows.Add (DR); }//Close connection odrreader.close (); Odcconnection.close (); Success = true; return DT; } catch {success = false; return DT; } } }}
This is what the sample code for C # Access database operations describes, and more about topic.alibabacloud.com (www.php.cn)!