access数据库帮助类

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

namespace SiteUrls
{
    public class OleDbHelper
    {
        public static OleDbCommand cmd = null;
        public static OleDbConnection conn = null;
        //
        public static string sApplicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

        string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sApplicationPath + "\\data\\DataInfo.accdb;";
        public static string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        public OleDbHelper()
        { }
        #region 建立数据库连接对象
        /// <summary>   
        /// 建立数据库连接   
        /// </summary>   
        /// <returns>返回一个数据库的连接OleDbConnection对象</returns>   
        public static OleDbConnection init()
        {
            try
            {
                conn = new OleDbConnection(connstr);
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }
            return conn;
        }
        #endregion

        #region 设置OleDbCommand对象
        /// <summary>   
        /// 设置OleDbCommand对象          
        /// </summary>   
        /// <param name="cmd">OleDbCommand对象 </param>   
        /// <param name="cmdText">命令文本</param>   
        /// <param name="cmdType">命令类型</param>   
        /// <param name="cmdParms">参数集合</param>   
        private static void SetCommand(OleDbCommand cmd, string cmdText, CommandType cmdType, OleDbParameter[] cmdParms)
        {
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            cmd.CommandType = cmdType;
            if (cmdParms != null)
            {
                cmd.Parameters.AddRange(cmdParms);
            }
        }
        #endregion

        #region 执行相应的OleDb语句,返回相应的DataSet对象
        /// <summary>   
        /// 执行相应的OleDb语句,返回相应的DataSet对象   
        /// </summary>   
        /// <param name="OleDbstr">OleDb语句</param>   
        /// <returns>返回相应的DataSet对象</returns>   
        public static DataSet GetDataSet(string OleDbstr)
        {
            DataSet set = new DataSet();
            try
            {
                init();
                OleDbDataAdapter adp = new OleDbDataAdapter(OleDbstr, conn);
                adp.Fill(set);
                conn.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }
            return set;
        }
        #endregion

        #region 执行相应的OleDb语句,返回相应的DataSet对象
        /// <summary>   
        /// 执行相应的OleDb语句,返回相应的DataSet对象   
        /// </summary>   
        /// <param name="OleDbstr">OleDb语句</param>   
        /// <param name="tableName">表名</param>   
        /// <returns>返回相应的DataSet对象</returns>   
        public static DataSet GetDataSet(string OleDbstr, string tableName)
        {
            DataSet set = new DataSet();
            try
            {
                init();
                OleDbDataAdapter adp = new OleDbDataAdapter(OleDbstr, conn);
                adp.Fill(set, tableName);
                conn.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }
            return set;
        }
    }
        #endregion
}

posted @ 2012-04-06 10:49  指教  阅读(194)  评论(0编辑  收藏  举报