using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using GotDotNet.ApplicationBlocks.Data;
using System.Collections;
namespace MyQuery.DAL
{
/// <summary>
/// 数据库访问封装 使用微软的AdoHelper
/// by 贾世义 2009-1-22
/// </summary>
public sealed class Dao
{
private const int TIMEOUT = 1000;
//在一个生命期内使用
private AdoHelper helper;
private IDbConnection iConnection;
private IDbTransaction iTransaction;
private DBType _dbtype = DBType.Sql2005;
/// <summary>
/// 处理的数据库类型
/// </summary>
public DBType Dbtype
{
get { return _dbtype; }
}
private string _connectionString = null;
/// <summary>
/// 构造函数 按照config配置的数据库处理
/// </summary>
public Dao() : this(WebHelper.GetDbType()) { }
/// <summary>
/// 构造函数 传入参数初始化数据库参数
/// </summary>
/// <param name="dbtype">数据库类型</param>
public Dao(DBType dbtype)
{
_dbtype = dbtype;
_connectionString = WebHelper.GetConnectionString(_dbtype);
helper = GetAdoHelper();
iConnection = helper.GetConnection(_connectionString);
}
/// <summary>
/// 构造函数 传入参数初始化数据库参数
/// </summary>
/// <param name="dbtype">数据库类型</param>
/// <param name="connectionString">连接字符串</param>
public Dao(DBType dbtype, string connectionString)
{
_dbtype = dbtype;
_connectionString = connectionString;
helper = GetAdoHelper();
iConnection = helper.GetConnection(_connectionString);
}
/*
/// <summary>
/// 析构函数
/// </summary>
~Dao()
{
if (iTransaction != null)
{
iTransaction.Dispose();
}
if (iConnection != null)
{
iConnection.Close();
iConnection.Dispose();
}
}*/
/// <summary>
/// 根据配置文件中的信息生成AdoHelper对象
/// </summary>
/// <returns></returns>
private AdoHelper GetAdoHelper()
{
switch (_dbtype)
{
case DBType.Sql2000:
case DBType.Sql2005:
return new GotDotNet.ApplicationBlocks.Data.SqlServer();
case DBType.Oracle:
return new GotDotNet.ApplicationBlocks.Data.Oracle();
case DBType.OleDb:
return new GotDotNet.ApplicationBlocks.Data.OleDb();
default:
return new GotDotNet.ApplicationBlocks.Data.Odbc();
}
}
/// <summary>
/// 开始执行事务(在事务提交前为一个完整的事务,即使多次开启也是同一事务)
/// </summary>
public void TransactionBegin()
{
if (iTransaction == null)
{
iConnection.Open();
iTransaction = iConnection.BeginTransaction();
}
else if (iConnection.State != ConnectionState.Open)
{
iConnection.Open();
}
}
/// <summary>
/// 提交事务
/// </summary>
/// <returns>是否成功</returns>
public bool TransactionCommit()
{
bool ret = true;
if (iTransaction != null)
{
try
{
iTransaction.Commit();
}
catch (Exception e)
{
iTransaction.Rollback();
ret = false;
throw e;
}
finally
{
iTransaction.Dispose();
iTransaction = null;
iConnection.Close();
}
}
return ret;
}
/// <summary>
/// 事务回滚
/// </summary>
/// <returns>是否成功</returns>
public bool TransactionRollback()
{
bool ret = true;
if (iTransaction != null)
{
try
{
iTransaction.Rollback();
}
catch (Exception e)
{
ret = false;
throw e;
}
finally
{
iTransaction.Dispose();
iTransaction = null;
iConnection.Close();
}
}
return ret;
}
/// <summary>
/// 根据输入的SQL语句得到DataSet对象
/// </summary>
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public DataSet GetDataSet(string sql, params IDataParameter[] parameters)
{
DataSet ds = new DataSet();
if (!String.IsNullOrEmpty(sql))
{
if (iTransaction != null)
{
helper.FillDataset(iTransaction, CommandType.Text, sql, ds, new string[] { }, parameters);
}
else
{
helper.FillDataset(iConnection, CommandType.Text, sql, ds, new string[] { }, parameters);
}
}
return ds;
}
/// <summary>
/// 根据输入的SQL语句得到DataSet对象
/// </summary>
/// <param name="sql">多余一条用;分割</param>
/// <param name="tableNames">对应表名</param>
/// <returns></returns>
public DataSet GetDataSet(string sql, string[] tableNames)
{
DataSet ds = new DataSet();
if (!String.IsNullOrEmpty(sql))
{
if (iTransaction != null)
{
helper.FillDataset(iTransaction, CommandType.Text, sql, ds, tableNames);
}
else
{
helper.FillDataset(iConnection, CommandType.Text, sql, ds, tableNames);
}
}
return ds;
}
/// <summary>
/// 根据SQL返回DataTable
/// </summary>
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns>不存在时为null</returns>
public DataTable GetDataTable(string sql, params IDataParameter[] parameters)
{
DataSet ds = GetDataSet(sql, parameters);
if (ds.Tables.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
/// <summary>
/// 按SQL语句获得指定DataReader对象(一定记得在使用完DataReader后关闭)
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="parameters"></param>
/// <returns>DataReader对象</returns>
public IDataReader GetDataReader(string sql, params IDataParameter[] parameters)
{
IDataReader dr = null;
if (!String.IsNullOrEmpty(sql))
{
if (iTransaction != null)
{
dr = helper.ExecuteReader(iTransaction, CommandType.Text, sql, parameters);
}
else
{
dr = helper.ExecuteReader(iConnection, CommandType.Text, sql, parameters);
}
}
return dr;
}
/// <summary>
/// 根据传入的SQL在数据库中执行SQl操作
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public int SqlExecute(string sql, params IDataParameter[] parameters)
{
if (String.IsNullOrEmpty(sql))
{
return -1;
}
else
{
if (iTransaction != null)
{
return helper.ExecuteNonQuery(iTransaction, CommandType.Text, sql, parameters);
}
else
{
return helper.ExecuteNonQuery(iConnection, CommandType.Text, sql, parameters);
}
}
}
/// <summary>
/// 批量执行SQL
/// </summary>
/// <param name="sqls">集合空语句忽略</param>
/// <returns></returns>
public bool SqlExecute(List<string> sqls)
{
bool ret = true;
IDbTransaction tran;
if (iTransaction == null)
{
iConnection.Open();
tran = iConnection.BeginTransaction();
}
else
{
tran = iTransaction;
}
try
{
for (int i = 0; i < sqls.Count; i++)
{
if (!String.IsNullOrEmpty(sqls[i]))
{
helper.ExecuteNonQuery(tran, CommandType.Text, sqls[i]);
}
}
if (iTransaction == null)
{
tran.Commit();
}
}
catch (Exception e)
{
if (iTransaction == null)
{
tran.Rollback();
}
ret = false;
throw e;
}
finally
{
if (iTransaction == null)
{
tran.Dispose();
iConnection.Close();
}
}
return ret;
}
/// <summary>
/// 执行查询,并返回查询结果集中的第一行,第一列的值,忽略其他列和行
/// </summary>
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public string GetScalar(string sql, params IDataParameter[] parameters)
{
object Result = null;
if (!String.IsNullOrEmpty(sql))
{
if (iTransaction != null)
{
Result = helper.ExecuteScalar(iTransaction, CommandType.Text, sql, parameters);
}
else
{
Result = helper.ExecuteScalar(iConnection, CommandType.Text, sql, parameters);
}
}
if (Result == null || Convert.IsDBNull(Result))
{
return "";
}
else
{
return Result.ToString();
}
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public string ProcedureExecute(string procName, params IDataParameter[] parameters)
{
string ret = string.Empty;
if (!String.IsNullOrEmpty(procName))
{
if (iTransaction == null)
{
iConnection.Open();
}
IDbCommand cmd = helper.CreateCommand(iConnection, procName, CommandType.StoredProcedure, parameters);
if (iTransaction != null)
{
cmd.Transaction = iTransaction;
}
cmd.CommandTimeout = TIMEOUT;
try
{
//返回值
IDataParameter RetVal = helper.GetParameter("RetVal", System.Data.DbType.Int32, 32, ParameterDirection.ReturnValue);
cmd.Parameters.Add(RetVal);
cmd.ExecuteNonQuery();
ret = RetVal.Value.ToString();
}
catch (Exception e)
{
throw e;
}
finally
{
cmd.Parameters.Clear();
cmd.Dispose();
if (iTransaction == null)
{
iConnection.Close();
}
}
}
return ret;
}
/// <summary>
/// 执行存储过程,返回DataSet
/// </summary>
/// <param name="procName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public DataSet GetDataSetProcedure(string procName, params IDataParameter[] parameters)
{
DataSet ds = new DataSet();
if (!String.IsNullOrEmpty(procName))
{
if (iTransaction == null)
{
iConnection.Open();
}
IDbCommand cmd = helper.CreateCommand(iConnection, procName, CommandType.StoredProcedure, parameters);
if (iTransaction != null)
{
cmd.Transaction = iTransaction;
}
cmd.CommandTimeout = TIMEOUT;
try
{
IDbDataAdapter da = helper.GetDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
}
catch (Exception e)
{
throw e;
}
finally
{
cmd.Parameters.Clear();
cmd.Dispose();
if (iTransaction == null)
{
iConnection.Close();
}
}
}
return ds;
}
/// <summary>
/// 执行存储过程,返回DataTable
/// </summary>
/// <param name="procName"></param>
/// <param name="parameters"></param>
/// <returns>不存在时为null</returns>
public DataTable GetDataTableProcedure(string procName, params IDataParameter[] parameters)
{
DataSet ds = GetDataSetProcedure(procName, parameters);
if (ds.Tables.Count > 0)
{
return ds.Tables[0];
}
return null;
}
}
}
欢迎访问:http://121.18.78.216 适易查询分析、工作流、内容管理及项目管理演示平台