C# 操作 access 資料庫

房上的貓發表於2018-03-26

 

隨筆:

(1)   名稱空間

            using System.Data.OleDb;

(2)   連線字串

            private staticstring connStr = @”Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:login.accdb”;

            注意:

             a、DataSource = 資料庫存放的路徑(這裡資料庫放到了D盤目錄下)

             b、 2003版本的Access資料庫連結字串:  privatestatic stringconnStr =@”Provider = Microsoft.Jet.OLEDB.4.0;DataSource = d:login.mdb”;

                   2007版本的Access資料庫連結字串:privatestaticstring connStr =@”Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:login.accdb”;

(3)   建立連線:

            OleDbConnection tempconn =new OleDbConnection(connStr);

(4)   使用OleDbCommand類來執行Sql語句:

           OleDbCommand cmd = new OleDbCommand(sql, tempconn);  

           tempconn.Open();

           cmd.ExecuteNonQuery();

 

 

栗子(工具類):

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.Show(tempconn.DataSource);

            tempconn.Open();

            MessageBox.Show(tempconn.State.ToString());

            return (tempconn);

        }

 

        /// <summary>

        /// 執行增加、刪除、修改指令

        /// </summary>

        /// <param name="sql">增加、刪除、修改的sql語句</param>

        /// <param name="param">sql語句的引數</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>

        /// 執行查詢指令,獲取返回的首行首列的值

        /// </summary>

        /// <param name="sql">查詢sql語句</param>

        /// <param name="param">sql語句的引數</param>

        /// <returns></returns>

        public static object ExecuteScalar(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.ExecuteScalar());

                }

            }

        }

 

        /// <summary>

        /// 執行查詢指令,獲取返回的datareader

        /// </summary>

        /// <param name="sql">查詢sql語句</param>

        /// <param name="param">sql語句的引數</param>

        /// <returns></returns>

        public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] param)

        {

            OleDbConnection conn = new OleDbConnection(connStr);

            OleDbCommand 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>

        /// 執行查詢指令,獲取返回datatable

        /// </summary>

        /// <param name="sql">查詢sql語句</param>

        /// <param name="param">sql語句的引數</param>

        /// <returns></returns>

        public static DataTable ExecuteDatable(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);

                    }

                    DataTable dt = new DataTable();

                    OleDbDataAdapter sda = new OleDbDataAdapter(cmd);

                    sda.Fill(dt);

                    return (dt);

                }

            }

 

        }

    }

}

 

 

 

 

 

相關文章