.Net下的数据持久层DAL
前几天正好解决了个通用数据持久访问层,特此拿出来奉献下~~~鲜花的鲜花,鸡蛋的鸡蛋~~~嘿嘿!
特点:
1。适合中小企业数据访问架构,其实大架构也没问题的,毕竟数据处理都放在了DB Server;
2。使用的数据工厂和泛型反射工厂,所以,支持目前所有的数据库系统,只要改改web.config就搞定了;
3。使用HashTable做条件字段的传输操作,并将值一个个传递给sp;
4。支持:获取数据集;添加数据(有返回结果);删除数据(有返回结果);
还有哦,这些是Ez原创的,转载请声明哦~~~
从这篇文章看作者还是下了功夫的, 但是对于参数的处理有点不太好,我觉得应该这样处理(_dbHelper):
using System;
using System.Data;//数据集
using System.Configuration;//连接字符串
using System.Collections;//数据字典
using System.Data.Common;//数据工厂
/**////
/// DBOperate 的摘要说明
/// Author:Ez
/// Date:2007-04-28
/// E-mail:zhaojun_free@hotmail.com
///
public class DBOperate
{
public DBOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
数据连接操作数据连接操作
/**////
/// 获取数据集,传入存储过程名strSPName + 条件组合htTemp
///
///
///
///
public DataSet GetData(string strSPName, Hashtable htTemp)
{
DataSet dsGetData = new DataSet();
try
{
using (DbConnection conn = this.GetDBConn())
{
DbCommand cmd = this.GetDBFactory().CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = strSPName;
if (htTemp != null)
{
foreach (DictionaryEntry deTemp in htTemp)
{
DbParameter param = this.GetDBFactory().CreateParameter();
param.ParameterName = deTemp.Key.ToString();
param.Value = deTemp.Value.ToString();
cmd.Parameters.Add(param);
}
}
DbDataAdapter da = this.GetDBFactory().CreateDataAdapter();
da.SelectCommand = cmd;
da.SelectCommand.Connection = conn;
da.Fill(dsGetData);
}
}
catch (Exception e)
{
throw new Exception("Fail to execute query: " + strSPName + ", Error:" + e.Message);
}
finally
{
dsGetData.Dispose();
}
return dsGetData;
}
/**////
/// 执行记录的插入、删除操作,返回值为Int
/// 1:操作成功;0:操作失败;
///
///
///
///
public int ExecuteSP(string strSPName,Hashtable htTemp)
{
try
{
using (DbConnection conn = this.GetDBConn())
{
conn.Open();
DbCommand cmd = this.GetDBFactory().CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = strSPName;
cmd.Connection = conn;
if (htTemp != null)
{
foreach (DictionaryEntry deTemp in htTemp)
{
DbParameter param = this.GetDBFactory().CreateParameter();
param.ParameterName = deTemp.Key.ToString();
param.Value = deTemp.Value.ToString();
cmd.Parameters.Add(param);
}
}
int iResult = cmd.ExecuteNonQuery();
return iResult;;
}
}
catch(Exception e)
{
throw new Exception("Fail to Execute SP:"+strSPName+",Error:"+e.Message);
}
}
}
using System.Data;//数据集
using System.Configuration;//连接字符串
using System.Collections;//数据字典
using System.Data.Common;//数据工厂
/**////
/// DBOperate 的摘要说明
/// Author:Ez
/// Date:2007-04-28
/// E-mail:zhaojun_free@hotmail.com
///
public class DBOperate
{
public DBOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
数据连接操作数据连接操作
/**////
/// 获取数据集,传入存储过程名strSPName + 条件组合htTemp
///
///
///
///
public DataSet GetData(string strSPName, Hashtable htTemp)
{
DataSet dsGetData = new DataSet();
try
{
using (DbConnection conn = this.GetDBConn())
{
DbCommand cmd = this.GetDBFactory().CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = strSPName;
if (htTemp != null)
{
foreach (DictionaryEntry deTemp in htTemp)
{
DbParameter param = this.GetDBFactory().CreateParameter();
param.ParameterName = deTemp.Key.ToString();
param.Value = deTemp.Value.ToString();
cmd.Parameters.Add(param);
}
}
DbDataAdapter da = this.GetDBFactory().CreateDataAdapter();
da.SelectCommand = cmd;
da.SelectCommand.Connection = conn;
da.Fill(dsGetData);
}
}
catch (Exception e)
{
throw new Exception("Fail to execute query: " + strSPName + ", Error:" + e.Message);
}
finally
{
dsGetData.Dispose();
}
return dsGetData;
}
/**////
/// 执行记录的插入、删除操作,返回值为Int
/// 1:操作成功;0:操作失败;
///
///
///
///
public int ExecuteSP(string strSPName,Hashtable htTemp)
{
try
{
using (DbConnection conn = this.GetDBConn())
{
conn.Open();
DbCommand cmd = this.GetDBFactory().CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = strSPName;
cmd.Connection = conn;
if (htTemp != null)
{
foreach (DictionaryEntry deTemp in htTemp)
{
DbParameter param = this.GetDBFactory().CreateParameter();
param.ParameterName = deTemp.Key.ToString();
param.Value = deTemp.Value.ToString();
cmd.Parameters.Add(param);
}
}
int iResult = cmd.ExecuteNonQuery();
return iResult;;
}
}
catch(Exception e)
{
throw new Exception("Fail to Execute SP:"+strSPName+",Error:"+e.Message);
}
}
}
public DbParameter CreateParameter()
{
DbParameter dbParameter;
dbParameter = _dataFactory.CreateParameter();
return dbParameter;
}
public string FormatParameter(string parameterName)
{
string formatMarkerParameter;
this.Open();
//DataSourceInformation : 此架构集合为 .NET Framework 管理的提供程序当前连接到的数据源公开有关信息。
//ParameterMarkerFormat : 表示如何格式化参数的格式化字符串。
formatMarkerParameter = _dbConnection.GetSchema("DataSourceInformation").
Rows[0]["ParameterMarkerFormat"].ToString();
this.Close();
return string.Format(formatMarkerParameter, parameterName);
}
调用可以这样:
DbParameter param = _dbHelper.CreateParameter();
param.ParameterName = _dbHelper.FormatParameter(PARAM_NAME);
param.DbType = DbType.String;
{
DbParameter dbParameter;
dbParameter = _dataFactory.CreateParameter();
return dbParameter;
}
public string FormatParameter(string parameterName)
{
string formatMarkerParameter;
this.Open();
//DataSourceInformation : 此架构集合为 .NET Framework 管理的提供程序当前连接到的数据源公开有关信息。
//ParameterMarkerFormat : 表示如何格式化参数的格式化字符串。
formatMarkerParameter = _dbConnection.GetSchema("DataSourceInformation").
Rows[0]["ParameterMarkerFormat"].ToString();
this.Close();
return string.Format(formatMarkerParameter, parameterName);
}
调用可以这样:
DbParameter param = _dbHelper.CreateParameter();
param.ParameterName = _dbHelper.FormatParameter(PARAM_NAME);
param.DbType = DbType.String;