`

Access 数据库封装类

阅读更多

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Collections;
using System.Configuration;

namespace Common.Dba
{
    public class DbAccessData:DbAccess
    {
       
        new OleDbCommand cmd = null;
        public DbAccessData()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            conn = new OleDbConnection();
            conn.ConnectionString = ConnectionString; //Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["datasource"]);
            //conn.ConnectionString = "initial catalog=idyan_new;data source=.;user id=bt;password=btbtbtbt;Connect Timeout=5000";
            cmd = new OleDbCommand();
            cmd.Connection = conn as OleDbConnection;
            cmd.CommandTimeout = 0;

        }
        public DbAccessData(string constr)
        {

            //
            // TODO: 在此处添加构造函数逻辑
            //
            conn = new OleDbConnection();
            //conn.ConnectionString = "initial catalog=pubs;data source=.;user id=sa;password=";
            //conn.ConnectionString = Convert.ToString(System.Configuration.ConfigurationSettings.AppSettings["datasource"]);
            conn.ConnectionString = constr;// "initial catalog=idyan_new;data source=.;user id=bt;password=btbtbtbt";
            cmd = new OleDbCommand();
            cmd.Connection = conn as OleDbConnection;
            cmd.CommandTimeout = 0;

        }
        /// <summary>
        /// 获取数据根据OleDb语句
        /// </summary>
        /// <param name="OleDb"></param>
        /// <returns></returns>
        public override DataTable GetTable(string sql)
        {
            DataSet ds = new DataSet();

            try
            {
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);
            }
            catch (Exception ex)
            {


                return null;

            }
            return ds.Tables[0] ?? new DataTable();
        }
        /// <summary>
        /// 获取数据根据sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public override DataSet GetDataSet(string sql)
        {
            DataSet ds = new DataSet();

            try
            {
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;

                da.Fill(ds);
            }
            catch (Exception ex)
            {


                return null;

            }
            return ds;
        }
        /// <summary>
        /// 获取数据根据sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public override DataSet GetDataSet(string sql, DbParameter[] pas)
        {
            DataSet ds = new DataSet();

            try
            {
                cmd.Parameters.Clear();
                cmd.CommandText = sql;
                foreach (OleDbParameter pa in pas)
                {
                    cmd.Parameters.Add(pa);
                }
                cmd.CommandType = CommandType.Text;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;

                da.Fill(ds);
            }
            catch (Exception ex)
            {


                return null;

            }
            return ds;
        }
        /// <summary>
        /// 获取数据根据sql语句 带参数 的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override DataTable GetTable(string sql, params DbParameter[] pas)
        {
            DataSet ds = new DataSet();
            try
            {
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                cmd.Parameters.Clear();

                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }


                da.Fill(ds);
            }
            catch (Exception ex)
            {


                return null;
            }
            return ds.Tables[0] ?? new DataTable();
        }
        /// <summary>
        /// 获取数据根据sql语句 带参数 的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override DataTable GetProcTable(string procname, params DbParameter[] pas)
        {
            DataSet ds = new DataSet();
            try
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = procname;
                //cmd.CommandText = sql;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                cmd.Parameters.Clear();

                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }

 

                da.Fill(ds);
            }
            catch (Exception ex)
            {


                return null;
            }
            return ds.Tables[0] ?? new DataTable();
        }
        /// <summary>
        /// 获取数据根据sql语句 带参数 的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override DataTable GetProcCursorTable(string procname, params DbParameter[] pas)
        {
            DataSet ds = new DataSet();
            try
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = procname;
                //cmd.CommandText = sql;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                cmd.Parameters.Clear();

                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }

 

                da.Fill(ds);
            }
            catch (Exception ex)
            {


                return null;
            }
            return ds.Tables[1] ?? new DataTable();
        }
        /// <summary>
        /// 获取数据根据sql语句 带参数 的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override int GetProcState(string procname, params DbParameter[] pas)
        {
            int state = 0;
            try
            {
                OpenConn();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = procname;
                cmd.Parameters.Clear();
                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }
                cmd.ExecuteNonQuery();
                CloseConn();
                state = Convert.ToInt32(pas[pas.Length - 1].Value);

            }
            catch
            {
                return 0;
            }
            return state;
        }
        /// <summary>
        /// 获取数据根据sql语句 带参数 的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override int GetProcStateNo(string procname, params DbParameter[] pas)
        {
            int state = 0;
            try
            {
                OpenConn();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = procname;
                cmd.Parameters.Clear();
                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }
                cmd.ExecuteNonQuery();
                CloseConn();
                state = 1;

                // state = Convert.ToInt32(pas[pas.Length - 1].Value);

            }
            catch
            {

                return 0;
            }
            return state;
        }
        /// <summary>
        /// 存储过程返回值的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override string GetProcStateReturnValue(string procname, params DbParameter[] pas)
        {
            string state = "";
            try
            {
                OpenConn();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = procname;
                cmd.Parameters.Clear();
                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }
                cmd.Parameters.AddWithValue("@Return_Value", "").Direction = ParameterDirection.ReturnValue;
                cmd.ExecuteNonQuery();
                state = Convert.ToString(cmd.Parameters["@Return_Value"].Value);
                state = state == null ? ("") : (state);
                CloseConn();


                // state = Convert.ToInt32(pas[pas.Length - 1].Value);

            }
            catch
            {

                return "";
            }
            return state;
        }
        /// <summary>
        /// 根据sql语句返回跟新状态
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public override bool GetState(string sql)
        {
            bool succ = false;
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                OpenConn();
                succ = cmd.ExecuteNonQuery() > 0 ? (true) : (false);
                CloseConn();
            }
            catch (Exception ex)
            {


                return false;
            }
            return succ;

        }
        /// <summary>
        /// 根据sql语句返回跟新状态带参数的
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="pas">参数的集合</param>
        /// <returns></returns>
        public override bool GetState(string sql, params DbParameter[] pas)
        {
            bool succ = false;
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Parameters.Clear();

                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }
                OpenConn();
                succ = cmd.ExecuteNonQuery() > 0 ? (true) : (false);
                CloseConn();
            }
            catch
            {

                // this.ShowError(ex.Message);
                //using (System.IO.StreamWriter sw = new System.IO.StreamWriter("D:\\error2008.txt"))
                //{
                //    sw.Write(ex.Message);
                //    sw.Flush();
                //}
                return false;
            }
            return succ;

        }
        /// <summary>
        /// 根据sql语句返回第一个单元格的数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public override string GetOne(string sql)
        {
            string res = "";
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                OpenConn();
                res = cmd.ExecuteScalar() == null ? ("") : (Convert.ToString(cmd.ExecuteScalar()));
                CloseConn();
            }
            catch (Exception ex)
            {


                return null;
            }
            return res;
        }
        /// <summary>
        ///  根据sql语句返回第一个单元格的数据带参数的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override string GetOne(string sql, params DbParameter[] pas)
        {
            string res = "";
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Parameters.Clear();

                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }
                OpenConn();
                res = cmd.ExecuteScalar() == null ? ("") : (Convert.ToString(cmd.ExecuteScalar()));
                CloseConn();
            }
            catch (Exception ex)
            {
                CloseConn();

                return null;
            }
            return res;
        }
        /// <summary>
        /// 返回数据的DataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public override DbDataReader GetDataReader(string sql)
        {
            OleDbDataReader dr = null;
            try
            {
                cmd.CommandType = CommandType.Text;
                conn.Open();
                cmd.CommandText = sql;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {


                return null;
            }
            return dr;
        }
        /// <summary>
        /// 返回数据的DataReader带参数的
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pas"></param>
        /// <returns></returns>
        public override DbDataReader GetDataReader(string sql, params DbParameter[] pas)
        {
            OleDbDataReader dr = null;
            try
            {
                cmd.CommandType = CommandType.Text;
                conn.Open();
                cmd.Parameters.Clear();

                foreach (OleDbParameter temppa in pas)
                {
                    cmd.Parameters.Add(temppa);
                }

                cmd.CommandText = sql;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {


                return null;
            }
            return dr;
        }
        /// <summary>
        /// 事务处理函数
        /// </summary>
        /// <param name="al"></param>
        /// <returns></returns>
        public override bool GetTranState(ArrayList al)
        {
            cmd.CommandType = CommandType.Text;
            OpenConn();
            OleDbTransaction tran = (conn as OleDbConnection).BeginTransaction();
            cmd.Transaction = tran;
            try
            {
                for (int i = 0; i < al.Count; i++)
                {
                    cmd.CommandText = Convert.ToString(al[i]);
                    cmd.ExecuteNonQuery();
                }

                tran.Commit();
                CloseConn();
            }
            catch
            {

                tran.Rollback();
                CloseConn();
                return false;
            }
            return true;

        }
      
        /// <summary>
        /// 分页函数
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="columns"></param>
        /// <param name="tablename"></param>
        /// <param name="pid"></param>
        /// <param name="order"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public override DataTable GetPageData(int current, int pagesize, string columns, string tablename, string pid, string where, string order)
        {
            current = current - 1 >= 0 ? (current - 1) : (0);
            string sql = string.Format("select top {0} {1} from {2} where 1=1 and {3} not in(select top {4}{3} from {2} where 1=1{5}  order by {6}){5} order by {6}", pagesize, columns, tablename, pid, current * pagesize, where, order);
            return GetTable(sql);
          
        }
        /// <summary>
        /// 分页存储过程的调用
        /// </summary>
        /// <param name="current"></param>
        /// <param name="pagesize"></param>
        /// <param name="columns"></param>
        /// <param name="tablename"></param>
        /// <param name="pid"></param>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public override DataTable GetProcPageData(int current, int pagesize, string columns, string tablename, string pid, string where, string order, string ordertype)
        {
            OleDbParameter[] pas = { new OleDbParameter("@PageIndex", current), new OleDbParameter("@PageSize", pagesize), new OleDbParameter("@Columns", columns), new OleDbParameter("@Tablename", tablename), new OleDbParameter("@Where", where), new OleDbParameter("@Order", order), new OleDbParameter("@OrderType", ordertype), new OleDbParameter("@Pid", pid) };
            return GetProcTable("Pages", pas);
            //current = current - 1 >= 0 ? (current - 1) : (0);
            //string sql = string.Format("select top {0} {1} from {2} where 1=1 and {3} not in(select top {4}{3} from {2} where 1=1{5}  order by {6}){5} order by {6}", pagesize, columns, tablename, pid, current * pagesize, where, order);
            //return GetTable(sql);
        }
        /// <summary>
        /// 分页存储过程的调用
        /// </summary>
        /// <param name="current"></param>
        /// <param name="pagesize"></param>
        /// <param name="columns"></param>
        /// <param name="tablename"></param>
        /// <param name="pid"></param>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public override DataTable GetProcData(int current, int pagesize, string columns, string tablename, string pid, string where, string order, string resultCount, string distinct)
        {
            OleDbParameter[] pas = { new OleDbParameter("@TableNames", OleDbType.VarChar, 200), new OleDbParameter("@PrimaryKey", OleDbType.VarChar, 100), new OleDbParameter("@Order", OleDbType.VarChar, 200), new OleDbParameter("@CurrentPage", OleDbType.Integer), new OleDbParameter("@PageSize", OleDbType.Integer), new OleDbParameter("@Fields", OleDbType.VarChar, 800), new OleDbParameter("@Filter", OleDbType.VarChar, 1000), new OleDbParameter("@ResultCount", OleDbType.VarChar, 12), new OleDbParameter("@distinct", OleDbType.VarChar, 12) };
            pas[0].Value = tablename;
            pas[1].Value = pid;
            pas[2].Value = order;
            pas[3].Value = current;
            pas[4].Value = pagesize;
            pas[5].Value = columns;
            pas[6].Value = where;
            pas[7].Value = resultCount;
            pas[8].Value = distinct;
            return this.GetProcTable("Pages", pas);
      
        }
        /// <summary>
        /// 分页存储过程的调用
        /// </summary>
        /// <param name="current"></param>
        /// <param name="pagesize"></param>
        /// <param name="columns"></param>
        /// <param name="tablename"></param>
        /// <param name="pid"></param>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public override DataTable GetProcAdminData(int current, int pagesize, string columns, string tablename, string pid, string where, string order, string resultCount, string distinct)
        {
            OleDbParameter[] pas = { new OleDbParameter("@TableNames", OleDbType.VarChar, 200), new OleDbParameter("@PrimaryKey", OleDbType.VarChar, 100), new OleDbParameter("@Order", OleDbType.VarChar, 200), new OleDbParameter("@CurrentPage", OleDbType.Integer), new OleDbParameter("@PageSize", OleDbType.Integer), new OleDbParameter("@Fields", OleDbType.VarChar, 800), new OleDbParameter("@Filter", OleDbType.VarChar, 200), new OleDbParameter("@ResultCount", OleDbType.VarChar, 24), new OleDbParameter("@Distinct", OleDbType.VarChar, 12) };
            pas[0].Value = tablename;
            pas[1].Value = pid;
            pas[2].Value = order;
            pas[3].Value = current;
            pas[4].Value = pagesize;
            pas[5].Value = columns;
            pas[6].Value = where;
            pas[7].Value = resultCount;
            pas[8].Value = distinct;
            return this.GetProcTable("Pages", pas);

        }
        public override DbParameter[] MakeParameters(params string[] str)
        {
            OleDbParameter[] pas = new OleDbParameter[str.Length / 2];
            for (int i = 0; i < str.Length / 2; i++)
            {
                pas[i] = new OleDbParameter(string.Format("@{0}",str[2*i]),str[2*i+1]);
            }
            return pas;
        }
       
    }
}

分享到:
评论

相关推荐

    C# ACCESS 数据库封装类 DBHELPER

    C# ACCESS 数据库封装类全部封装完毕 大家 拿去用吧

    C# ACCESS 数据库操作类

    总结来说,"C# ACCESS 数据库操作类"是一个用于简化C#程序中与Access数据库交互的自定义类,它通过封装ADO.NET组件提供了高效且易于使用的数据库操作接口。"AccessHelper.cs"文件包含了实现这些功能的源代码,是理解...

    ACCESS数据库类 ACCESS源码 C#ACCESS数据库操作 C#ACCESS数据库源

    根据给定的信息,我们可以深入探讨以下几个关键的...综上所述,`OleDbHelper`类为C#应用程序提供了高效且灵活的方式以操作ACCESS数据库,通过封装最佳实践,简化了数据库操作的复杂性,提高了代码的可读性和可维护性。

    c#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)

    ### c#.NET万能数据库访问封装类:ACCESS、SQLServer、Oracle 在现代软件开发中,数据持久化是至关重要的一个环节,而数据库操作作为数据持久化的基础,其效率和可维护性直接影响到整个应用程序的性能和稳定性。...

    C#数据库操作类AccessHelper实例

    综上所述,AccessHelper是C#中处理Access数据库的一个实用工具,通过封装数据库连接和SQL执行,提供了方便且安全的数据库操作接口。在实际开发中,可以根据需要扩展这个类,添加更多功能,如参数化查询、存储过程...

    Access数据库的简单封装

    DsRecodeDB.cpp和DsRecodeDB.h可能是封装Access数据库的类定义和实现。DsRecodeDB类可能包含以下方法: - `connect()`: 连接数据库,初始化ODBC或ADO连接。 - `disconnect()`: 断开与数据库的连接。 - `executeSQL...

    ado_class.rar_ ADO_class_ACCESS 封装类_ADO数据库_ado封装类_数据库封装类

    在给定的“ado_class.rar”压缩包中,包含了一个名为“ADO_class_ACCESS”的封装类,用于简化对Access数据库的操作。下面将详细解释ADO、封装类的概念以及如何利用这些文件进行数据库操作。 **ADO(ActiveX Data ...

    Access 数据库操作封装类库

    对于"Access数据库操作封装类库",我们可以设计一个名为`DBAccess`的类,它包含以下几个主要功能: 1. **连接数据库**:`ConnectDatabase`方法,负责建立与Access数据库的连接,通常需要输入数据库的路径和连接字符...

    c#NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)文库[归类].pdf

    本文档主要介绍了一个使用C#语言开发的万能数据库访问封装类,該类支持ACCESS、SQLServer、Oracle等多种数据库管理系统。该类提供了统一的数据库访问接口,讓开发者可以轻松地访问不同类型的数据库。 首先,类...

    ADO_QT_qtaccess_ADO_ACCESS数据库

    描述中提到“QT通过ADO形式操作access数据库的封装类,亲测可用。”这表明已经有人实现了一个QT类,该类封装了ADO接口,用于与ACCESS数据库进行通信,并且这个实现已经被验证是有效的。 在标签中,“QT”指代框架...

    ASP.NET 2.0 C# Access数据库访问类

    ### ASP.NET 2.0 C# Access数据库访问类详解 #### 概述 在Web开发领域,特别是使用ASP.NET框架进行开发时,有效地管理和操作数据库是必不可少的一部分。本篇文章将详细介绍一个针对ASP.NET 2.0环境下的C#语言编写...

    ACCESS数据库C#操作类

    该类通过抽象方法封装了与ACCESS数据库交互的细节,使得开发者可以更专注于业务逻辑而非底层数据库操作。 #### 使用配置连接字符串 `DbHelperACE`类中定义了一个静态属性`connectionString`,用于存储与ACCESS...

    Access数据库 ADO的封装

    在"Access数据库 ADO的封装"中,开发者通常会创建一个类或模块,将这些ADO对象的常用操作抽象出来,形成一套接口。例如,封装可能包含以下功能: 1. 连接管理:创建、打开和关闭数据库连接,处理连接字符串。 2. ...

    ADO.zip_ado 封装类_ado封装类_数据库封装类

    本篇将详细讲解标题为“ADO.zip_ado 封装类_ado封装类_数据库封装类”的压缩包所包含的ADO封装类,以及其在数据库连接中的应用。 首先,了解ADO的基本概念。ADO是.NET框架早期版本中的数据访问技术,它提供了与数据...

    C#ACCESS数据库操作类源码

    源码中的“ACCESS数据库操作类”可能封装了以下功能: 1. 数据库连接:类可能会有一个静态或非静态的成员变量,用于存储数据库连接字符串。连接字符串包含了数据库的位置、用户名、密码等信息,例如:"Provider=...

    C++操作Access数据库示例

    同时,为了提高代码的可读性和复用性,可以将数据库操作封装到一个类或函数库中。 通过上述步骤,你可以在C++中实现对Access数据库的基本操作,如查询、插入、更新和删除数据。如果你有更复杂的需求,比如事务处理...

    Access数据库php操作类

    1. **数据库连接**:类中通常包含一个`connect`方法,用于建立与Access数据库的连接。它可能需要数据库的路径、用户名和密码(如果设置了)作为参数。连接成功后,会返回一个数据库连接句柄。 2. **查询操作**:类...

    C++操作Access数据库(ADO)VS2010 MFC

    在本文中,我们将深入探讨如何使用C++的MFC(Microsoft Foundation Classes)框架与ADO(ActiveX Data Objects)在Visual Studio 2010中操作Access数据库。这是一篇适合初学者的技术指南,旨在帮助你了解如何通过...

    MFC对Access数据库操作实例

    在MFC中,我们可以使用`CDaoDatabase`类来创建一个新的Access数据库。首先,需要创建一个`CDaoDatabase`对象,然后调用其`Create`成员函数,指定数据库文件的路径。例如: ```cpp CDaoDatabase db; db.Create(L...

    易语言access数据库中添加与读取图片

    为了提高代码的可复用性和可维护性,可以将上述操作封装成函数或类,比如`添加图片到数据库`和`从数据库读取图片`的函数。 通过以上步骤,你可以在易语言环境下实现Access数据库中图片的添加与读取。理解这些基本...

Global site tag (gtag.js) - Google Analytics