AccessHelper 需修改

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace CyObject.BLL
{
    public class DataAccessDAL

    {
        protected static OleDbConnection conn = new OleDbConnection();
        protected static OleDbCommand comm = new OleDbCommand();
        public DataAccessDAL()
        {
            //init
        }

        /// <summary>
        /// 打开数据库
        /// </summary>
        private static void openConnection()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["myconn"]);//web.config文件里设定。             
                comm.Connection = conn;
                try
                {
                    conn.Open();
                }
                catch (Exception e)
                { throw new Exception(e.Message); }

            }

        }

        /// <summary>
        /// 关闭数据库
        /// </summary>
        private static void closeConnection()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
                comm.Dispose();
            }
        }
        public static object ExecuteScalar(string sql, params OleDbParameter[] parameters) {
            openConnection();
            comm.CommandText = sql;
            comm.Parameters.AddRange(parameters);
            return comm.ExecuteScalar();
        }

        public static int ExecuteNonQuery(string sql, params OleDbParameter[] parameters) {
            openConnection();
            comm.CommandText = sql;
            comm.Parameters.AddRange(parameters);
            return comm.ExecuteNonQuery();
        }

        public static DataTable ExecuteDataTable(string sql, params OleDbParameter[] parameters) {
            openConnection();
            comm.CommandText = sql;
            comm.Parameters.AddRange(parameters);
            DataSet dataset = new DataSet();
            OleDbDataAdapter apdater = new OleDbDataAdapter(comm);
            apdater.Fill(dataset);
            return dataset.Tables[0];
        }

    }
}

  

posted @ 2016-01-22 17:01  少时不知贵  阅读(282)  评论(0编辑  收藏  举报