/* 2008 4 25 更新 */
我的数据访问函数库的源码。整个类有1400多行,原先就是分开来写的,现在更新后还是分开来发一下吧。
第一部分:内部成员,初始化设置等。
using System;
using System.Data;
using System.Data.SqlClient;
using HBS.DataStruct;
using HBS.Form;
//using System.Security.Principal;
namespace HBS
{
/// <summary>
/// 存储过程的参数的类型,在输出型的参数里使用。
/// </summary>
public enum ParameterKind
{
Int,Double,Decimal,NVarChar,Bit
}
/// <summary>
/// 这是一个通用的数据访问层接口。对ADO.NET的封装。功能类似于 SQLHelper ,但是需要实例化。
/// </summary>
public sealed class DataAccessLayer
{
#region 属性
private string errorMsg; //出错信息
private static bool isShowErrorSQL; //是否显示出错的查询语句(包括存储过程名程)
private int executeRowCount; //获取执行SQL查询语句后影响的行数
private SqlCommand cm ; //建立Command对象
private SqlTransaction sqlTrans ; //用于事务处理
public bool isUseTrans; //是否启用了 .net 的事务处理
/// <summary>
/// 读取出错信息,用于判断是否出现异常
/// </summary>
public string ErrorMsg
{
get{return errorMsg;}
}
/// <summary>
/// 修改连接字符串,在同时访问两个或两个以上的数据库的时候使用。限于同一类型的数据库,这里是SQL Server
/// </summary>
public string cnString
{
set{cm.Connection.ConnectionString = value;}
get{return cm.Connection.ConnectionString;}
}
/// <summary>
/// 获取执行SQL查询语句后影响的行数
/// </summary>
public int ExecuteRowCount
{
get{return executeRowCount;}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (isUseTrans)
sqlTrans.Dispose();
errorMsg = null;
cm.Parameters.Clear();
cm.Connection.Close();
cm.Dispose();
}
#endregion
public DataAccessLayer() //构造函数
{
//默认不使用事务
isUseTrans = false;
//得到 SqlCommand 的实例
cm = new SqlCommand();
//获取连接字符串
cm.Connection = new SqlConnection(HBS.Config.Connection.ConnectionString );
//初始化错误信息
errorMsg = "";
isShowErrorSQL = true; //本地运行,显示出错的查询语句(包括存储过程名程)
//isShowErrorSQL = false; //服务器运行,不显示出错的查询语句(包括存储过程名程)
}
#region 内部函数
//设置初始值
/// <summary>
/// 设置 errorMsg = "0" ;cm.CommandText 和 cm.CommandType
/// </summary>
/// <param name="commandText">查询语句或者存储过程</param>
/// <param name="commandType">1:存储过程;2:查询语句</param>
private void SetCommand(string commandText,int commandType)
{
errorMsg = ""; //清空错误信息
executeRowCount = ;
cm.CommandText = commandText;
if (commandType == )
cm.CommandType = CommandType.Text;
else
cm.CommandType = CommandType.StoredProcedure;
}
//设置出错信息
/// <summary>
/// 当发生异常时,所作的处理
/// </summary>
/// <param name="FunctionName">函数名称</param>
/// <param name="commandText">查询语句或者存储过程</param>
/// <param name="message">错误信息</param>
private void SetErrorMsg(string FunctionName,string commandText,string message)
{
//设置返回给调用者的错误信息
errorMsg = FunctionName + "函数出现错误。<BR>错误信息:" + message;
if (isShowErrorSQL ) errorMsg += "<BR>查询语句:" + commandText ;
if (isUseTrans)
{
this.TranRollBack(); //事务模式下:自动回滚事务,不用调用者回滚
}
cm.Connection.Close(); //关闭连接
addLogErr(commandText,errorMsg); //记录到错误日志
}
#endregion
#region 记录错误日志
//如果要使用的话,根据你的需要进行修改。
public void addLogErr(string SPName,string ErrDescribe)
{
//记录到错误日志
string FilePath = System.Web.HttpContext.Current.Server.MapPath("/log/" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
System.Text.StringBuilder str = new System.Text.StringBuilder();
str.Append(DateTime.Now.ToString());
str.Append("\t");
str.Append(System.Web.HttpContext.Current.Request.Url.PathAndQuery);
str.Append("\r\n");
str.Append(SPName);
str.Append("\r\n");
str.Append(ErrDescribe.Replace("<BR>",""));
if (isUseTrans)
{
str.Append("\r\n");
str.Append("启动事务下出现异常!");
isUseTrans = false; //修改事务标志。设置为不使用事务
}
str.Append("\r\n\r\n");
System.IO.StreamWriter sw = null;
try
{
sw = new System.IO.StreamWriter(FilePath,true,System.Text.Encoding.Unicode );
sw.Write(str.ToString());
}
catch(Exception ex)
{
System.Web.HttpContext.Current.Response.Write("没有访问日志文件的权限!或日志文件只读!");
}
finally
{
if (sw != null)
sw.Close();
}
}
#endregion
//事务日志
#region 事务处理部分。并没有做太多的测试,有不合理的地方请多指教
/// <summary>
/// 打开连接,并且开始事务。
/// </summary>
public void TranBegin()
{
cm.Connection.Open(); //打开连接,直到回滚事务或者提交事务。
sqlTrans = cm.Connection.BeginTransaction(); //开始一个事务
cm.Transaction = sqlTrans; //交给Command
isUseTrans = true; //标记为启用事务
}
/// <summary>
/// 提交事务,并关闭连接
/// </summary>
public void TranCommit()
{
if (isUseTrans)
{
sqlTrans.Commit(); //提交事务
cm.Connection.Close(); //关闭连接
isUseTrans = false; //修改事务标志。
}
else
{
//没有启用事务,或者已经回滚,或者已经提交了事务
addLogErr("误操作","在没有启用事务,或者已经回滚,或者已经提交了事务的情况下再次提交事务。请注意查看程序流程!");
}
}
/// <summary>
/// 回滚事务,并关闭连接。在程序出错的时候,自动调用。
/// </summary>
public void TranRollBack()
{
if (isUseTrans)
{
sqlTrans.Rollback(); //回滚事务
cm.Connection.Close(); //关闭连接
isUseTrans = false; //修改事务标志。
}
else
{
//没有启用事务,或者已经回滚,或者已经提交了事务
addLogErr("误操作","在没有启用事务,或者已经回滚,或者已经提交了事务的情况下再次回滚事务。请注意查看程序流程!");
}
}
#endregion
}
}
下载全部源文件。