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 );}}}}}