C # operate the access database,

Source: Internet
Author: User

C # operate the access database,

 

Essay:

(1) namespace

Using System. Data. OleDb;

(2) connection string

Private staticstring connStr = @ "Provider = Microsoft. Ace. OLEDB.12.0; Data Source = d: \ login. accdb ";

Note:

A. DataSource = database storage path (the database is stored in the d directory)

B. Access database link string of Version 2003: privatestatic stringconnStr = @ "Provider = Microsoft. Jet. OLEDB.4.0; DataSource = d: \ login. mdb ";

2007 Access Database Link string: privatestaticstring connStr = @ "Provider = Microsoft. Ace. OLEDB.12.0; Data Source = d: \ login. accdb ";

(3) establish a connection:

OleDbConnection tempconn = new OleDbConnection (connStr );

(4) use the OleDbCommand class to execute SQL statements:

OleDbCommand cmd = new OleDbCommand (SQL, tempconn );

Tempconn. Open ();

Cmd. ExecuteNonQuery ();

 

 

Chestnuts:

Using System; using System. collections. generic; using System. linq; using System. text; using System. data. oleDb; using System. windows. forms; using System. data; namespace test {class AccessHelper {private static string connStr = @ "Provider = Microsoft. ace. OLEDB.12.0; Data Source = d: \ login. accdb "; public static OleDbConnection GetConn () {OleDbConnection tempconn = new OleDbConnection (connStr); MessageBox. S How (tempconn. dataSource); tempconn. open (); MessageBox. show (tempconn. state. toString (); return (tempconn );} /// <summary> /// execute add, delete, and modify commands /// </summary> /// <param name = "SQL"> add, delete, and modify SQL statement </param> /// <param name = "param"> SQL statement parameters </param> /// <returns> </returns> public static int ExecuteNonQuery (string SQL, params OleDbParameter [] param) {using (OleDbConnection conn = new OleDbConnection (connStr) {Using (OleDbCommand cmd = new OleDbCommand (SQL, conn) {if (param! = Null) {cmd. parameters. addRange (param);} conn. open (); return (cmd. executeNonQuery () ;}}/// <summary> /// execute the query command, obtain the value of the first column of the returned line. /// </summary> /// <param name = "SQL"> query SQL statement </param> /// <param name =" param "> SQL statement parameters </param> /// <returns> </returns> public static object ExecuteScalar (string SQL, params OleDbParameter [] param) {using (OleDbConnection conn = new OleDbConnection (connStr) {using (Ole DbCommand cmd = new OleDbCommand (SQL, conn) {if (param! = Null) {cmd. parameters. addRange (param);} conn. open (); return (cmd. executeScalar () ;}}/// <summary> /// execute the query command, obtain the returned datareader /// </summary> /// <param name = "SQL"> query SQL statement </param> /// <param name = "param"> SQL statement parameters </param> /// <returns> </returns> public static OleDbDataReader ExecuteReader (string SQL, params OleDbParameter [] param) {OleDbConnection conn = new OleDbConnection (connStr); OleDbCo Mmand cmd = conn. CreateCommand (); cmd. CommandText = SQL; cmd. CommandType = CommandType. Text; if (param! = Null) {cmd. parameters. addRange (param);} conn. open (); return (cmd. executeReader (CommandBehavior. closeConnection);} // <summary> // execute the query command, get the returned datatable // </summary> /// <param name = "SQL"> query SQL statement </param> /// <param name = "param"> SQL statement parameters </param> /// <returns> </returns> public static DataTable ExecuteDatable (string SQL, params OleDbParameter [] param) {using (OleDbConnection conn = new OleDbC Onnection (connStr) {using (OleDbCommand cmd = new OleDbCommand (SQL, conn) {if (param! = Null) {cmd. parameters. addRange (param);} DataTable dt = new DataTable (); OleDbDataAdapter sda = new OleDbDataAdapter (cmd); sda. fill (dt); return (dt );}}}}}

 

 

 

 

 

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.