C#的Access数据库操作类


关键词

C# Access 数据库 类

摘要

本文介绍一个洪哥自己写的C#操作Access数据库的类,很简单,很好用。

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

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


下面是具体的代码:


using System;
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

 

文章的版权

洪哥写文章很苦逼,如果本文对您略有帮助,点击感兴趣的广告支持洪哥!万分感谢!本文属于“洪哥笔记”原创文章,转载请注明来源地址:C#的Access数据库操作类:http://www.splaybow.com/post/csharp-access-class.html

关注与收藏

如果您在服务器运维、网络管理、网站或系统开发过程中有任何疑问,洪哥以此为饭碗,请加QQ:115085382!
了解更多好文章,请关注我们的新浪微博和腾讯微博
亲,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D


« ASP.NET中上传并读取Excel数据库 C#窗体按回车符的响应按钮 »