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];
} }
}