连接ACCESS数据库类

using System;
using System.Data;
using System.Data.OleDb;

namespace Bussiness.Cls
{
    /// <summary>
    /// UtilHelper 的摘要说明
    /// </summary>
    public class UtilHelper
    {
        public UtilHelper()
        { }

        public static string ConnString = @"Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + Application.StartupPath + "//AnalyseFile//DB//DownLoad.mdb" + ";Persist Security Info=False;Jet OLEDB:Database Password=;";

        private static string Password = "xyz_@#$%^9&8*4()!_+ooyq#$%";

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static OleDbDataReader ExecuteReader(string sql)
        {
            try
            {
                //创建数据库连接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    //创建command对象并保存sql查询语句
                    OleDbCommand command = new OleDbCommand(sql, conn);

                    //创建datareader 对象来连接到表单
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        return reader;
                    }
                }
            }
            //一些通常的异常处理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] oledbparams)
        {
            try
            {
                //创建数据库连接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    //创建command对象并保存sql查询语句
                    OleDbCommand command = new OleDbCommand(sql, conn);
                    // 增加参数
                    if (oledbparams != null)
                    {
                        foreach (OleDbParameter p in oledbparams)
                        {
                            if (p != null)
                            {
                                if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && (p.Value == null))
                                {
                                    p.Value = DBNull.Value;
                                }
                                command.Parameters.Add(p);
                            }
                        }
                    }
                    //创建datareader 对象来连接到表单
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        return reader;
                    }
                }
            }
            //一些通常的异常处理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        public static void ExecuteNonQuery(string sql)
        {
            try
            {
                //创建数据库连接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    //创建command对象并保存sql查询语句
                    OleDbCommand command = new OleDbCommand(sql, conn);
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    //创建datareader 对象来连接到表单
                    command.ExecuteNonQuery();
                    conn.Close();
                }
            }
            //一些通常的异常处理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        public static void ExecuteNonQuery(string sql, params OleDbParameter[] oledbparams)
        {
            try
            {
                //创建数据库连接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {

                    //创建command对象并保存sql查询语句
                    //    OleDbCommand command = new OleDbCommand(sql, conn);
                    OleDbCommand command = conn.CreateCommand();
                    command.CommandText = sql;
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    command.CommandType = CommandType.Text;
                    // 增加参数
                    if (oledbparams != null)
                    {
                        for (int i = 0; i < oledbparams.Length; i++)
                        {
                            command.Parameters.Add(oledbparams[i]);

                        }
                    }

                    //创建datareader 对象来连接到表单
                    command.ExecuteNonQuery();
                    conn.Close();
                }
            }
            //一些通常的异常处理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string sql)
        {
            try
            {
                //创建数据库连接
                using (OleDbConnection conn = new OleDbConnection(ConnString))
                {
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    using (OleDbDataAdapter ida = new OleDbDataAdapter(sql, conn))
                    {
                        DataSet ds = new DataSet();
                        ida.Fill(ds);
                        return ds;
                    }
                }
            }
            //一些通常的异常处理
            catch (OleDbException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }       
    }

展开阅读全文

没有更多推荐了,返回首页