最近在整理以前的资料时,看到了以前我们在项目中经常用的一个数据库访问类,虽然现在已经可以用代码生成工具生成比较完整的数据库访问类,但是这个类在我们以前的项目中久经考验,所以我觉得还是比较好用,废话不多说了,上代码:
//======================================================================
//
// filename : DataBaseAccess.cs
//
// description: 1. data base access operation class DataBaseAccess.
// 2. data base access operation help class SQLHelper.
//
//
//
//======================================================================
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
namespace DAL
{
#region Database Access
///
/// DataBase Operate Class DataBaseAccess
///
public class DataBaseAccess
{
///
/// DataBase Connection
///
private SqlConnection conn = new SqlConnection(SQLHelper.StrConn);
///
/// DataBase Connection
///
public SqlConnection Conn
{
get
{
return conn;
}
}
///
/// Construct
///
public DataBaseAccess()
{
}
///
/// Destruct
///
~DataBaseAccess()
{
CloseDB();
}
#region "***** Debug Configuration *****"
///
///Judge whether the state is Debug
///
/// if the state is Debug return true,else false
private bool JudgeDebug()
{
bool bIsDebug = false;
#if DEBUG
string strIsDebug = ConfigurationManager.AppSettings["IsDebug"];
bIsDebug = ((bIsDebug || (strIsDebug != null && strIsDebug.Equals("true"))) true : false);
#endif
return bIsDebug;
}
///
/// Output the Debug Information
///
/// Debug Information
private void debug(object objDebugInfo)
{
#if DEBUG
//if open debug,output the debug information into the file(the Directory in which Current programe run and the file name is DebugInfo\[日期].ini)
if (JudgeDebug())
{
string strPath = System.Environment.CurrentDirectory + "\\DebugInfo\\";
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
try
{
StreamWriter swDebugOutput = new StreamWriter(strPath + DateTime.Now.ToLongDateString() + ".ini", true, System.Text.Encoding.Unicode);
swDebugOutput.Write("time:" + DateTime.Now.ToString() + "\r\n" + objDebugInfo + "\r\n\r\n");
swDebugOutput.Close();
swDebugOutput.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
#endif
}
#endregion
#region "***** Database Basic Operation *****"
#region ExecuteSql
///
/// Execute SQL(insert,delete,update)command,return the number of the rows which are affected
///
/// SQL Command which will be Executed
/// re