使用C#来连接并操作Access数据库,洪哥把它封装成了一个类。

这个类主要有如下一些功能:连接数据库、打开数据库连接、关闭数据库连接、执行SQL语句、ExecuteScalar(可返回数字、字符串等不同类型的值)、获取DataTable等。

using System.Collections.Generic;

using System.Text;

using System.Data.OleDb;

using System.Data;

    public class DbAccess

    {

        public static string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + CConfig.DbPath;

        public static string ErrorMessage = "";

        private static OleDbConnection objConnection = new OleDbConnection();

        private static OleDbCommand objCommand = new OleDbCommand();

        /// <summary>

        /// Open Database

        /// </summary>

        /// <returns>return True while success, otherwise False</returns>

        private static bool OpenConn()

        {

            objConnection.ConnectionString = ConnectionString;

            try

            {

                objConnection.Open();

                return true;

            }

            catch (Exception ex)

            {

                ErrorMessage = ex.ToString();

                return false;

            }

        }

        /// <summary>

        /// Close Database

        /// </summary>

        private static void CloseConn()

        {

            if (objConnection.State == System.Data.ConnectionState.Open)

            {

                try

                {

                    objConnection.Close();

                }

                catch { }

            }

        }

        /// <summary>

        /// ExecuteNonQuery

        /// </summary>

        /// <param name="strSql">input, SQL statement</param>

        /// <param name="intCount">output, Lines affected while executing the SQL</param>

        /// <returns>True/success, False/fail</returns>

        public static bool ExecuteNonQuery(string strSql, out int intCount)

        {

            intCount = -1;

            objCommand.CommandType = System.Data.CommandType.Text;

            objCommand.CommandText = strSql;

            if (OpenConn())

            {

                objCommand.Connection = objConnection;

                intCount = objCommand.ExecuteNonQuery();

                CloseConn();

                return true;

            }

            else

            {

                return false;

            }

        }

        /// <summary>

        /// Get Row 1 Column 1, Integer

        /// </summary>

        /// <param name="strSql"></param>

        /// <param name="i"></param>

        /// <returns></returns>

        public static bool ExecuteScalar(string strSql, out int i)

        {

            i = -1;

            objCommand.CommandType = System.Data.CommandType.Text;

            objCommand.CommandText = strSql;

            if (OpenConn())

            {

                objCommand.Connection = objConnection;

                i = (int)objCommand.ExecuteScalar();

                CloseConn();

                return true;

            }

            else

            {

                return false;

            }

        }

        /// <summary>

        /// Get Row 1 Column 1, String

        /// </summary>

        /// <param name="strSql"></param>

        /// <param name="str"></param>

        /// <returns></returns>

        public static bool ExecuteScalar(string strSql, out string str)

        {

            str = null;

            objCommand.CommandType = System.Data.CommandType.Text;

            objCommand.CommandText = strSql;

            if (OpenConn())

            {

                objCommand.Connection = objConnection;

                Object o = objCommand.ExecuteScalar();

                if (o is DBNull)

                {

                    str = "";

                }

                else

                {

                    str = o.ToString();

                }

                CloseConn();

                return true;

            }

            else

            {

                return false;

            }

        }

        /// <summary>

        /// Get DataTable

        /// </summary>

        /// <param name="sql"></param>

        /// <returns></returns>

        public static DataTable GetDataSet(string sql)

        {

            DataSet dataset = new DataSet();

            objCommand.CommandType = CommandType.Text;

            objCommand.CommandText = sql;

            if (OpenConn())

            {

                objCommand.Connection = objConnection;

                OleDbDataAdapter da = new OleDbDataAdapter(objCommand);

                da.Fill(dataset);

                CloseConn();

            }

            return dataset.Tables[0];

        }

    }

ExecuteScalarExecuteScalar

03-14 12:31