Database Operation Notes

Manipulate Access database

OleDbConnection usage

            string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=testDB.accdb";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();//Open
            //conn.Close();//Close


       //open data connection
        public static OleDbConnection OpenConnection()
        {
            OleDbConnection conn = new OleDbConnection(strConn);
            try
            {
                conn.Open();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Open database exception: {0}",ex.Message);
            }

            return conn;
        }

OleDbCommand usage

        //Execute the SQL statement and return the number of rows affected
        public static int ExecuteNonQuery(string strCmd)
        {
            int rows = 0;
            OleDbConnection conn = OpenConnection();
            if(conn.State == System.Data.ConnectionState.Open)
            {
                try
                {
                    OleDbCommand comm = new OleDbCommand(strCmd, conn);
                    rows = comm.ExecuteNonQuery();//Returns the number of rows affected
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }

            return rows;
        }
        //Execute the SQL statement and return the number of rows affected
        // pass parameters
        public static int ExecuteNonQuery(string strCmd, OleDbParameter[] values)
        {
            int rows = 0;
            OleDbConnection conn = OpenConnection();
            if(conn.State == System.Data.ConnectionState.Open)
            {
                try
                {
                    OleDbCommand cmd = new OleDbCommand(strCmd, conn);
                    cmd.Parameters.AddRange(values);
                    rows = cmd.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }

            return rows;
        }

DataTable usage

       // get the data table
        public static DataTable GetDataTable(string strCmd)
        {
            DataTable table = new DataTable();
            OleDbConnection conn = OpenConnection();
            if(conn.State == ConnectionState.Open)
            {
                try
                {
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strCmd, conn);
                    adapter.Fill(table);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("GetDataTable:" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }

            return table;
        }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324843264&siteId=291194637