Sample code Introduction to C # Access database operations

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

  • Related Article

    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.