C#编写的Access数据库的DBHelp

因为需要…所以它就产生了…新鲜出炉还没怎么测试.至少编译是没有问题的…

下面是代码:

/*
 * By :落落
 * URL: Www.MyLuoLuo.Com
 */ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace DBHelp
{
    /// <summary>
    /// Access Help
    /// </summary>
    public class Access
    {
        #region 小功能
        /// <summary>
        /// 数据库访问
        /// </summary>
        OleDbConnection conn;
        string conString;
        public OleDbConnection con
        {
            get {
                if (conn == null)
                {
                    conn = new OleDbConnection(conString);
                }
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();

                }
                return conn;
            }
        }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        /// <param name="conString"></param>
        public Access(string conString)
        {
            try
            {
                conn = new OleDbConnection(conString);
                this.conString = conString;
                conn.Open();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 填充
        /// </summary>
        /// <param name="com"></param>
        /// <param name="array"></param>
        void SetParametersArray(ref OleDbCommand com, ParametersArray array)
        {
            foreach (OleDbParameter item in array.GetArray())
            {
                com.Parameters.Add(item);
            }
        }
        #endregion
        #region 执行SQL语句并返回受影响的行数
        /// <summary>
        /// 执行SQL语句并返回受影响的行数
        /// </summary>
        /// <returns></returns>
        public int ExecuteNonQuery(string sql)
        {
            try
            {
                using (OleDbCommand com = new OleDbCommand(sql, con))
                {
                    return com.ExecuteNonQuery();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 执行SQL语句并返回受影响的行数
        /// </summary>
        public int ExecuteNonQuery(string sql, OleDbParameter par)
        {
            try
            {
                using (OleDbCommand com = new OleDbCommand(sql, con))
                {
                    com.Parameters.Add(par);
                    return com.ExecuteNonQuery();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 执行SQL语句并返回受影响的行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="array"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string sql, ParametersArray array)
        {
            try
            {
                OleDbCommand com = new OleDbCommand(sql, con);
                SetParametersArray(ref com, array);
                return com.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region ExecuteReader
        public OleDbDataReader ExecuteReader(string sql)
        {
            try
            {
                using (OleDbCommand com = new OleDbCommand(sql, con))
                {
                    OleDbDataReader reader = com.ExecuteReader();
                    return reader;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public OleDbDataReader ExecuteReader(string sql, OleDbParameter par)
        {
            try
            {
                using (OleDbCommand com = new OleDbCommand(sql, con))
                {
                    com.Parameters.Add(par);
                    OleDbDataReader reader = com.ExecuteReader();
                    return reader;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public OleDbDataReader ExecuteReader(string sql, ParametersArray array)
        {
            try
            {
                OleDbCommand com = new OleDbCommand(sql, con);
                SetParametersArray(ref com, array);
                OleDbDataReader reader = com.ExecuteReader();
                return reader;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 读取查询结果中的第一行第一列
        /// <summary>
        /// 读取查询结果中的第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExecuteScalar(string sql)
        {
            try
            {
                using (OleDbCommand com = new OleDbCommand(sql, con))
                {
                    return com.ExecuteScalar();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 读取查询结果中的第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExecuteScalar(string sql, OleDbParameter par)
        {
            try
            {
                using (OleDbCommand com = new OleDbCommand(sql, con))
                {
                    com.Parameters.Add(par);
                    return com.ExecuteScalar();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 读取查询结果中的第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExecuteScalar(string sql, ParametersArray array)
        {
            try
            {
                OleDbCommand com = new OleDbCommand(sql, con);
                SetParametersArray(ref com, array);
                return com.ExecuteScalar();
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 返回DataSet
        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet GetDataSet(string sql)
        {
            try
            {

                using (OleDbDataAdapter adpter = new OleDbDataAdapter(sql, con))
                {
                    DataSet ds = new DataSet();
                    adpter.Fill(ds);
                    return ds;
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet GetDataSet(string sql,OleDbParameter par)
        {
            try
            {
                using (OleDbCommand com = new OleDbCommand(sql,con))
                {
                    com.Parameters.Add(par);
                    using (OleDbDataAdapter adpter = new OleDbDataAdapter(com))
                    {
                        DataSet ds = new DataSet();
                        adpter.Fill(ds);
                        return ds;
                    }
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet GetDataSet(string sql, ParametersArray array)
        {
            try
            {
                OleDbCommand com = new OleDbCommand(sql, con);
                SetParametersArray(ref com,array);
                using (OleDbDataAdapter adpter = new OleDbDataAdapter(com))
                {
                    DataSet ds = new DataSet();
                    adpter.Fill(ds);
                    return ds;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
    }

    /// <summary>
    /// 一组Parameters对象
    /// </summary>
    public class ParametersArray
    {
        List<System.Data.OleDb.OleDbParameter> par = new List<System.Data.OleDb.OleDbParameter>();
        /// <summary>
        /// 添加新参数
        /// </summary>
        /// <param name="par"></param>
        public void Add(System.Data.OleDb.OleDbParameter par)
        {
            this.par.Add(par);
        }
        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        public List<System.Data.OleDb.OleDbParameter> GetArray()
        {
            return this.par;
        }
    }
}

[buy]出错请留言~[/buy]

[download id=”36″]

2 Replies to “C#编写的Access数据库的DBHelp”

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据