- 浏览: 815582 次
-
文章分类
最新评论
-
centralplains:
坑爹其他代码呢???
C#获取CPU占用率、内存占用、磁盘占用、进程信息 -
hellodajun:
;支持你;Come on ! me too!
2012注定是收获的一年,奋斗才刚刚开始 -
xiaomogui:
楼主买那么多书, 要花多少钱??!!!
2012注定是收获的一年,奋斗才刚刚开始 -
pengchenming:
挺好的,向楼主学习,自己刚做完计划,但是看看楼主的,发现自己需 ...
2012注定是收获的一年,奋斗才刚刚开始 -
pacer123:
难道是炫耀贴
2012注定是收获的一年,奋斗才刚刚开始
[C#]分享一个以前的项目使用的DataBaseAccess类
最近在整理以前的资料时,看到了以前我们在项目中经常用的一个数据库访问类,虽然现在已经可以用代码生成工具生成比较完整的数据库访问类,但是这个类在我们以前的项目中久经考验,所以我觉得还是比较好用,废话不多说了,上代码:
//======================================================================
//
// 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
/// <summary>
/// DataBase Operate Class DataBaseAccess
/// </summary>
public class DataBaseAccess
{
/// <summary>
/// DataBase Connection
/// </summary>
private SqlConnection conn = new SqlConnection(SQLHelper.StrConn);
/// <summary>
/// DataBase Connection
/// </summary>
public SqlConnection Conn
{
get
{
return conn;
}
}
/// <summary>
/// Construct
/// </summary>
public DataBaseAccess()
{
}
/// <summary>
/// Destruct
/// </summary>
~DataBaseAccess()
{
CloseDB();
}
#region "***** Debug Configuration *****"
/// <summary>
///Judge whether the state is Debug
/// </summary>
/// <returns>if the state is Debug return true,else false</returns>
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;
}
/// <summary>
/// Output the Debug Information
/// </summary>
/// <param name="objDebugInfo">Debug Information</param>
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
/// <summary>
/// Execute SQL(insert,delete,update)command,return the number of the rows which are affected
/// </summary>
/// <param name="sqlcmd">SQL Command which will be Executed</param>
/// <returns>return the number of the rows which are affected</returns>
public int ExecuteSql(SqlCommand sqlcmd)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSql(SqlCommand),Return Type:int ");
this.conn.Open();
sqlcmd.Connection = this.conn;
SqlTransaction trans = this.conn.BeginTransaction();
sqlcmd.Transaction = trans;
try
{
debug("Execute SQL Command:" + sqlcmd.CommandText);
int iReturnValue = sqlcmd.ExecuteNonQuery();
trans.Commit();
return iReturnValue;
}
catch (SqlException ex)
{
debug("Exception Information:" + ex.ToString());
trans.Rollback();
throw ex;
}
finally
{
sqlcmd.Dispose();
this.conn.Close();
}
}
/// <summary>
/// Execute SQL(insert,delete,update)command,return the number of the rows which are affected
/// </summary>
/// <param name="sqlcmd">SQL Command which will be Executed</param>
/// <returns>return the number of the rows which are affected</returns>
public int ExecuteSql(string strSql)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSql(string),Return Type:int ");
return ExecuteSql(new SqlCommand(strSql,this.conn));
}
/// <summary>
/// Execute SQL(insert,delete,update)command,return the number of the rows which are affected.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="sqlParameters">SQL Parameter</param>
/// <returns>return the number of the rows which are affected</returns>
public int ExecuteSql(string strSql, SqlParameter[] sqlParameters)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSql(string, SqlParameter[]),Return Type:int ");
return ExecuteSql(SQLHelper.CreateCommand(strSql, sqlParameters, this.conn));
}
#endregion
#region ExecuteSqlDic
/// <summary>
/// Execute mutil-SQL(insert,delete,update)command,keep an affair.
/// </summary>
/// <param name="sqlcmd">SQL Command collection which will be Executed</param>
/// <param name="bNotAffectRowRollback">if true,once one SQL Execute,the result of the execution is invalid,if false,ignore the result and rollback.</param>
/// <returns>return the list number of the rows which are affected</returns>
public List<int> ExecuteSqlDic(Dictionary<string, SqlParameter[]> dic, bool bNotAffectRowRollback)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDic(Dictionary<string, SqlParameter[]>, bool),Return Type:List<int> ");
List<int> iReturnValueList = new List<int>();
this.conn.Open();
SqlTransaction trans = this.conn.BeginTransaction();
try
{
foreach (KeyValuePair<string, SqlParameter[]> kvp in dic)
{
SqlCommand sqlcmd = SQLHelper.CreateCommand(kvp.Key, kvp.Value, this.conn);
sqlcmd.Transaction = trans;
debug("Execute SQL Command:" + sqlcmd.CommandText);
int iAffectRow=sqlcmd.ExecuteNonQuery();
iReturnValueList.Add(iAffectRow);
if (bNotAffectRowRollback && iAffectRow == 0)
{
trans.Rollback();
iReturnValueList.Clear();
return iReturnValueList;
}
}
trans.Commit();
}
catch (SqlException ex)
{
debug("Exception Information:" + ex.ToString());
trans.Rollback();
throw ex;
}
finally
{
this.conn.Close();
}
return iReturnValueList;
}
/// <summary>
/// Execute mutil-SQL(insert,delete,update)command,keep an affair.
/// </summary>
/// <param name="sqlcmd">SQL Command collection which will be Executed</param>
/// <returns>return the list number of the rows which are affected</returns>
public List<int> ExecuteSqlDic(Dictionary<string, SqlParameter[]> dic)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDic(Dictionary<string, SqlParameter[]>),Return Type:List<int> ");
return ExecuteSqlDic(dic, false);
}
#endregion
#region
/// <summary>
/// Execute SQL Command,Return single Result.
/// </summary>
/// <param name="sqlcmd">SQL Command collection which will be Executed</param>
/// <returns>return single Result</returns>
public object ExecScalar(SqlCommand sqlcmd)
{
debug("Now Execute DataBaseAccess's Method:ExecScalar(SqlCommand),Return Type:object ");
sqlcmd.Connection = this.conn;
try
{
debug("Execute SQL Command:" + sqlcmd.CommandText);
this.conn.Open();
object r = sqlcmd.ExecuteScalar();
//if (Object.Equals(r, null))
//{
// throw new Exception("object is null!");
//}
//else
//{
// return r;
//}
return r;
}
catch (SqlException ex)
{
debug("Exception Information:" + ex.ToString());
throw ex;
}
finally
{
sqlcmd.Dispose();
this.conn.Close();
}
}
/// <summary>
/// Execute SQL Command,Return single Result.
/// </summary>
/// <param name="sqlcmd">SQL Command collection which will be Executed</param>
/// <returns>return single Result</returns>
public object ExecScalar(string strSql)
{
debug("Now Execute DataBaseAccess's Method:ExecScalar(string),Return Type:object ");
return ExecScalar(new SqlCommand(strSql,this.conn));
}
/// <summary>
/// Execute SQL Command,Return single Result.
/// </summary>
/// <param name="sqlcmd">SQL Command collection which will be Executed</param>
/// <param name="sqlParameters">SQL Parameters Collection</param>
/// <returns>return single Result</returns>
public object ExecScalar(string strSql, SqlParameter[] sqlParameters)
{
debug("Now Execute DataBaseAccess's Method:ExecScalar(string,SqlParameter[]),Return Type:object ");
return ExecScalar(SQLHelper.CreateCommand(strSql, sqlParameters, this.conn));
}
#endregion
#region ExecScalarEx
/// <summary>
/// Execute SQL command,if result set has note return 1,if result set is null return 0
/// </summary>
/// <param name="sqlcmd">SQL Command which will be Executed</param>
/// <returns>Execute SQL command,if result set has note return 1,if result set is null return 0</returns>
public int ExecScalarEx(SqlCommand sqlcmd)
{
debug("Now Execute DataBaseAccess's Method:ExecScalarEx(SqlCommand),Return Type:int ");
sqlcmd.Connection = this.conn;
try
{
debug("Execute SQL Command:" + sqlcmd.CommandText);
this.conn.Open();
SqlDataReader myDr = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
if (myDr.Read())
{
return 1;
}
else
{
return 0;
}
}
catch (SqlException ex)
{
debug("Exception Information:" + ex.ToString());
throw ex;
}
finally
{
sqlcmd.Dispose();
this.conn.Close();
}
}
/// <summary>
/// Execute SQL command,if result set has note return 1,if result set is null return 0
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <returns>Execute SQL command,if result set has note return 1,if result set is null return 0</returns>
public int ExecScalarEx(string strSql)
{
debug("Now Execute DataBaseAccess's Method:ExecScalarEx(strSql),Return Type:int ");
return ExecScalarEx(new SqlCommand(strSql, this.conn));
}
/// <summary>
/// Execute SQL command,if result set has note return 1,if result set is null return 0
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="sqlParameters">SQL Command Collection</param>
/// <returns>Execute SQL command,if result set has note return 1,if result set is null return 0</returns>
public int ExecScalarEx(string strSql, SqlParameter[] sqlParameters)
{
debug("Now Execute DataBaseAccess's Method:ExecScalarEx(string,SqlParameter[]),Return Type:int ");
return ExecScalarEx(SQLHelper.CreateCommand(strSql, sqlParameters, this.conn));
}
#endregion
#region ExecuteSqlDs
/// <summary>
/// Execute SQL Command,return DataSet.
/// </summary>
/// <param name="sqlcmd">SQL Command which will be Executed</param>
/// <param name="strTableName">table name</param>
/// <returns>return DataSet.</returns>
public DataSet ExecuteSqlDs(SqlCommand sqlcmd, string strTableName)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDs(SqlCommand,string),Return Type:DataSet ");
sqlcmd.Connection = this.conn;
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
DataSet dsReturn = new DataSet();
try
{
debug("Execute SQL Command:" + sqlcmd.CommandText);
this.conn.Open();
sqlda.Fill(dsReturn, strTableName);
return dsReturn;
}
catch (SqlException ex)
{
debug("Exception information:" + ex.ToString());
throw ex;
}
finally
{
sqlcmd.Dispose();
sqlda.Dispose();
this.conn.Close();
}
}
/// <summary>
/// Execute SQL Command,return DataSet.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="strTableName">table name</param>
/// <returns>return dataset.</returns>
public DataSet ExecuteSqlDs(string strSql, string strTableName)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDs(string,string),Return Type:DataSet ");
return ExecuteSqlDs(new SqlCommand(strSql, this.conn), strTableName);
}
/// <summary>
/// Execute SQL Command,return DataSet.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="sqlParameters">SQL Parameter Collection</param>
/// <param name="strTableName">table name</param>
/// <returns>return DataSet.</returns>
public DataSet ExecuteSqlDs(string strSql, SqlParameter[] sqlParameters, string strTableName)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDs(string,SqlParameter[],string),Return Type:DataSet ");
return ExecuteSqlDs(SQLHelper.CreateCommand(strSql, sqlParameters, this.conn), strTableName);
}
#endregion
#region ExecuteSqlFillDs
/// <summary>
/// Execute SQL Command,add new resultset into current ref DataSet.
/// </summary>
/// <param name="sqlcmd">SQL Command which will be Executed</param>
/// <param name="strTableName">table name</param>
/// <param name="dsRef">current Dataset</param>
public void ExecuteSqlFillDs(SqlCommand sqlcmd, string strTableName, ref DataSet dsRef)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlFillDs(SqlCommand,string,ref DataSet)");
sqlcmd.Connection = this.conn;
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
try
{
debug("Execute SQL Command:" + sqlcmd.CommandText);
this.conn.Open();
sqlda.Fill(dsRef, strTableName);
}
catch (SqlException ex)
{
debug("Exception information:" + ex.ToString());
throw ex;
}
finally
{
sqlcmd.Dispose();
sqlda.Dispose();
this.conn.Close();
}
}
/// <summary>
/// Execute SQL Command,add new resultset into current ref DataSet.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="strTableName">table name</param>
/// <param name="dsRef">current Dataset</param>
public void ExecuteSqlFillDs(string strSql, string strTableName, ref DataSet dsRef)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlFillDs(string,string,ref DataSet)");
ExecuteSqlFillDs(new SqlCommand(strSql),strTableName, ref dsRef);
}
/// <summary>
/// Execute SQL Command,add new resultset into current ref DataSet.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="sqlParameters">SQL Parameters Collection</param>
/// <param name="strTableName">table name</param>
/// <param name="dsRef">Current Dataset</param>
public void ExecuteSqlFillDs(string strSql, SqlParameter[] sqlParameters, string strTableName, ref DataSet dsRef)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlFillDs(string strSql, SqlParameter[], string, ref DataSet)");
ExecuteSqlFillDs(SQLHelper.CreateCommand(strSql, sqlParameters, this.conn), strTableName, ref dsRef);
}
#endregion
#region ExecuteSqlDsEx
/// <summary>
/// Define pagination(Execute SQL Command,return DataSet).
/// </summary>
/// <param name="sqlcmd">SQL Command which will be Executed</param>
/// <param name="iStartRecord">index of StartRecord</param>
/// <param name="iMaxRecord">number of Records</param>
/// <param name="strTableName">table name</param>
/// <returns>return DataSet</returns>
public DataSet ExecuteSqlDsEx(SqlCommand sqlcmd, int iStartRecord, int iMaxRecord, string strTableName)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDsEx(SqlCommand,int,int,string),Return Type:DataSet ");
sqlcmd.Connection = this.conn;
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
DataSet dsReapter = new DataSet();
try
{
debug("Execute SQL Command:" + sqlcmd.CommandText);
this.conn.Open();
if (iStartRecord < 0) iStartRecord = 0;
sqlda.Fill(dsReapter, iStartRecord, iMaxRecord, strTableName);
return dsReapter;
}
catch (SqlException ex)
{
debug("Exception information:" + ex.ToString());
throw ex;
}
finally
{
sqlcmd.Dispose();
sqlda.Dispose();
this.conn.Close();
}
}
/// <summary>
/// Define pagination(Execute SQL Command,return DataSet).
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="iStartRecord">index of StartRecord</param>
/// <param name="iMaxRecord">number of Records</param>
/// <param name="strTableName">table name</param>
/// <returns>return DataSet.</returns>
public DataSet ExecuteSqlDsEx(string strSql, int iStartRecord, int iMaxRecord, string strTableName)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDsEx(string,int,int,string),Return Type:DataSet ");
return ExecuteSqlDsEx(new SqlCommand(strSql), iStartRecord, iMaxRecord, strTableName);
}
/// <summary>
/// Define pagination(Execute SQL Command,return DataSet).
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="sqlParameters">SQL Parameters Collection</param>
/// <param name="iStartRecord">index of StartRecord</param>
/// <param name="iMaxRecord">number of Records</param>
/// <param name="strTableName">table name</param>
/// <returns>return DataSet.</returns>
public DataSet ExecuteSqlDsEx(string strSql, SqlParameter[] sqlParameters, int iStartRecord, int iMaxRecord, string strTableName)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDsEx(string, SqlParameter[], int, int, string),Return Type:DataSet ");
return ExecuteSqlDsEx(SQLHelper.CreateCommand(strSql, sqlParameters, this.conn), iStartRecord, iMaxRecord, strTableName);
}
#endregion
#region ExecuteSqlDr
/// <summary>
/// Execute SQL Command,return SqlDataReader.
/// </summary>
/// <param name="sqlcmd">SQL Command which will be Executed</param>
/// <returns>Return SqlDataReader</returns>
public SqlDataReader ExecuteSqlDr(SqlCommand sqlcmd)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDr(SqlCommand),Return Type:SqlDataReader ");
sqlcmd.Connection = this.conn;
SqlDataReader sqldr;
try
{
debug("Execute SQL Command:" + sqlcmd.CommandText.ToString());
this.conn.Open();
sqldr = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
return sqldr;
}
catch (SqlException ex)
{
debug("Exception information:" + ex.ToString());
throw ex;
}
finally
{
sqlcmd.Dispose();
}
}
/// <summary>
/// Execute SQL Command,return SqlDataReader.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <returns>Return SqlDataReader</returns>
public SqlDataReader ExecuteSqlDr(string strSql)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDr(string),Return Type:SqlDataReader ");
return ExecuteSqlDr(new SqlCommand(strSql));
}
/// <summary>
/// Execute SQL Command,return SqlDataReader.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="sqlParameters">SQL Parameters Collection</param>
/// <returns>Return SqlDataReader</returns>
public SqlDataReader ExecuteSqlDr(string strSql, SqlParameter[] sqlParameters)
{
debug("Now Execute DataBaseAccess's Method:ExecuteSqlDr(string, SqlParameter[]),Return Type:SqlDataReader ");
return ExecuteSqlDr(SQLHelper.CreateCommand(strSql, sqlParameters, this.conn));
}
#endregion
#region ExecuteSqlTarn
/// <summary>
/// Execute SQL Command,Keep affair.
/// </summary>
/// <param name="strSql">SQL Command which will be Executed</param>
/// <param name="sqlParameters">SQL Parameters Collection</param>
public void ExecuteSqlTran(string strSql, SqlParameter[] sqlParameters)
{
conn.Open();
SqlCommand sqlcmd = SQLHelper.CreateCommand(strSql, sqlParameters, this.conn);
using(SqlTransaction sqltrans = this.conn.BeginTransaction())
{
sqlcmd.Transaction = sqltrans;
try
{
sqlcmd.ExecuteNonQuery();
sqltrans.Commit();
}
catch (System.Data.SqlClient.SqlException E)
{
sqltrans.Rollback();
throw E;
}
finally
{
sqlcmd.Dispose();
this.conn.Close();
}
}
}
/// <summary>
/// execute SQL script,Keep SqlTransaction 。
/// </summary>
/// <param name="objSqlList">save sql command and sql parameter</param>
public void ExecuteSqlTran(Dictionary<string ,SqlParameter []> objSqlList)
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
try
{
SqlCommand sqlcmd = new SqlCommand();
//circulation
foreach (KeyValuePair<string, SqlParameter[]> kvp in objSqlList)
{
//the key value is by|Division ,Serial number|sql script
string[] tmp = kvp.Key.ToString().Split(new char[] { '|' });
string cmdText = tmp[1];
//get SqlParameter value
SqlParameter[] sqlParms = kvp.Value;
if (sqlParms!=null)
sqlcmd = SQLHelper.CreateCommand(cmdText, sqlParms, this.conn);
sqlcmd.Transaction = trans;
int val = sqlcmd.ExecuteNonQuery();
//clear SqlParameter
sqlcmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
finally
{
this.conn.Close();
}
}
}
#endregion
#endregion
#region other
/// <summary>
///Close DataBase Connection.
/// </summary>
public void CloseDB()
{
if (this.conn != null)
{
if (this.conn.State != ConnectionState.Closed)
this.conn.Close();
}
}
/// <summary>
/// Dispose Resource
/// </summary>
public void Dispose()
{
if (this.conn != null)
{
if (this.conn.State != ConnectionState.Closed)
this.conn.Close();
this.conn.Dispose();
}
}
#endregion
}
#endregion
#region DataBase Operate assistant class SQLHelper
/// <summary>
/// SQLHelper.
/// </summary>
public abstract class SQLHelper
{
/// <summary>
/// DataBase ConnectionString
/// </summary>
public static string StrConn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
//------------------------------------------------------------------------------------------------------------
/// <summary>
/// Create SqlParameter.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="DbType">One of the System.Data.SqlDbType values.</param>
/// <param name="value">The length of the parameter.</param>
/// <returns>Return new SqlParameter.</returns>
public static SqlParameter CreateSqlParameter(string parameterName, SqlDbType DbType, object value)
{
SqlParameter sqlpara = new SqlParameter(parameterName, DbType);
sqlpara.Value = value;
return sqlpara;
}
/// <summary>
/// Create SqlParameter.
/// </summary>
/// <param name="parameterName">The name of the parameter to map.</param>
/// <param name="DbType">One of the System.Data.SqlDbType values.</param>
/// <returns>Return new SqlParameter.</returns>
public static SqlParameter CreateSqlParameter(string parameterName, SqlDbType DbType, int size, object value)
{
SqlParameter sqlpara = new SqlParameter(parameterName, DbType, size);
sqlpara.Value = value;
return sqlpara;
}
//------------------------------------------------------------------------------------------------------------
/// <summary>
/// Param amortize Hashtable
/// </summary>
private static Hashtable htParamCache = Hashtable.Synchronized(new Hashtable());
/// <summary>
/// Save Parameters in Cache
/// </summary>
/// <param name="strCacheKey"></param>
/// <param name="sqlParameters"></param>
public static void CacheParameters(string strCacheKey, params SqlParameter[] sqlParameters)
{
SQLHelper.htParamCache[strCacheKey] = sqlParameters;
}
/// <summary>
/// Get Parameters from Cache
/// </summary>
/// <param name="strCacheKey"></param>
/// <returns></returns>
public static SqlParameter[] GetCachedParameters(string strCacheKey)
{
SqlParameter[] sqlParameters = (SqlParameter[])SQLHelper.htParamCache[strCacheKey];
if (sqlParameters == null)
{
return null;
}
SqlParameter[] clonedParms = new SqlParameter[sqlParameters.Length];
for (int i = 0, j = sqlParameters.Length; i < j; i++)
{
clonedParms[i] = (SqlParameter)((ICloneable)sqlParameters[i]).Clone();
}
return clonedParms;
}
//----------------------------------------------------------------------------------------------------------------
/// <summary>
/// Create new SqlComand
/// </summary>
/// <param name="strSql"></param>
public static SqlCommand CreateCommand(string strSql)
{
SqlCommand sqlcmd = new SqlCommand(strSql);
return sqlcmd;
}
/// <summary>
/// Create new SqlComand,Set DataBase Connection
/// </summary>
/// <param name="strSql"></param>
/// <param name="sqlconn"></param>
/// <returns></returns>
public static SqlCommand CreateCommand(string strSql, SqlConnection sqlconn)
{
SqlCommand sqlcmd = new SqlCommand(strSql, sqlconn);
return sqlcmd;
}
/// <summary>
/// Create new SqlComand which has Parameters
/// </summary>
/// <param name="strSql"></param>
/// <param name="sqlParameters"></param>
public static SqlCommand CreateCommand(string strSql, SqlParameter[] sqlParameters)
{
SqlCommand sqlcmd = new SqlCommand(strSql);
foreach (SqlParameter param in sqlParameters)
{
sqlcmd.Parameters.Add(param);
}
return sqlcmd;
}
/// <summary>
/// Create new SqlComand which has Parameters,Set DataBase Connection
/// </summary>
/// <param name="strSql"></param>
/// <param name="conn"></param>
/// <param name="sqlParameters"></param>
public static SqlCommand CreateCommand(string strSql, SqlParameter[] sqlParameters, SqlConnection sqlconn)
{
SqlCommand sqlcmd = new SqlCommand(strSql, sqlconn);
foreach (SqlParameter param in sqlParameters)
{
sqlcmd.Parameters.Add(param);
}
return sqlcmd;
}
/// <summary>
/// Create new SqlComand which has Parameters,Set Stored Procedure Flag
/// </summary>
/// <param name="strSql"></param>
/// <param name="sqlParameters"></param>
/// <param name="bIsStoredProcedure"></param>
/// <returns></returns>
public static SqlCommand CreateCommand(string strSql, SqlParameter[] sqlParameters, bool bIsStoredProcedure)
{
SqlCommand sqlcmd = new SqlCommand(strSql);
if (bIsStoredProcedure)
sqlcmd.CommandType = CommandType.StoredProcedure;
else
sqlcmd.CommandType = CommandType.Text;
foreach (SqlParameter param in sqlParameters)
{
sqlcmd.Parameters.Add(param);
}
return sqlcmd;
}
/// <summary>
/// Create new SqlComand which has Parameters,Set Stored Procedure Flag and DataBase Connection
/// </summary>
/// <param name="strSql"></param>
/// <param name="sqlParameters"></param>
/// <param name="bIsStoredProcedure"></param>
/// <returns></returns>
public static SqlCommand CreateCommand(string strSql, SqlParameter[] sqlParameters, bool bIsStoredProcedure, SqlConnection sqlconn)
{
SqlCommand sqlcmd = new SqlCommand(strSql, sqlconn);
if (bIsStoredProcedure)
sqlcmd.CommandType = CommandType.StoredProcedure;
else
sqlcmd.CommandType = CommandType.Text;
foreach (SqlParameter param in sqlParameters)
{
sqlcmd.Parameters.Add(param);
}
return sqlcmd;
}
/// <summary>
/// Create new SqlDataAdapter which has Parameters and set DataBase Connection
/// </summary>
/// <param name="sqlda"></param>
/// <param name="mySqlParamter"></param>
public static SqlDataAdapter CreateDataAdapter(string strSql, SqlParameter[] sqlParameters, SqlConnection sqlconn)
{
SqlDataAdapter sqlda = new SqlDataAdapter(strSql, sqlconn);
foreach (SqlParameter param in sqlParameters)
{
sqlda.SelectCommand.Parameters.Add(param);
}
return sqlda;
}
/// <summary>
/// Create new SqlDataAdapter which has Parameters,Set Stored Procedure Flag and DataBase Connection
/// </summary>
/// <param name="strSql"></param>
/// <param name="mySqlParamter"></param>
/// <param name="bIsStoredProcedure"></param>
/// <returns></returns>
public static SqlDataAdapter CreateDataAdapter(string strSql, SqlParameter[] sqlParameters, bool bIsStoredProcedure, SqlConnection sqlconn)
{
SqlDataAdapter sqlda = new SqlDataAdapter(strSql, sqlconn);
if (bIsStoredProcedure)
sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
else
sqlda.SelectCommand.CommandType = CommandType.Text;
foreach (SqlParameter param in sqlParameters)
{
sqlda.SelectCommand.Parameters.Add(param);
}
return sqlda;
}
/// <summary>
/// Create SqlParameter[]
/// </summary>
/// <param name="sqlParameterArr"></param>
/// <returns></returns>
public static SqlParameter[] CreateSqlParameters(object[,] sqlParameterArr)
{
SqlParameter[] sqlParameters = new SqlParameter[sqlParameterArr.GetLength(0)];
int i = 0;
foreach (SqlParameter param in sqlParameters)
{
sqlParameters[i] = new SqlParameter(Convert.ToString(sqlParameterArr[i, 0]), sqlParameterArr[i, 1]);
i++;
}
return sqlParameters;
}
/// <summary>
/// add Parameters for Command
/// </summary>
/// <param name="sqlcmd"></param>
/// <param name="mySqlParamter"></param>
public static void AddCommandParams(ref SqlCommand sqlcmd, SqlParameter[] sqlParameters)
{
foreach (SqlParameter param in sqlParameters)
{
sqlcmd.Parameters.Add(param);
}
}
/// <summary>
/// add Parameters for DataAdapter
/// </summary>
/// <param name="sqlda"></param>
/// <param name="mySqlParamter"></param>
public static void AddDataAdapterParam(ref SqlDataAdapter sqlda, SqlParameter[] sqlParameters)
{
foreach (SqlParameter param in sqlParameters)
{
sqlda.SelectCommand.Parameters.Add(param);
}
}
/// <summary>
/// Get SQLScript
/// </summary>
/// <param name="strFilepath"></param>
/// <param name="strNodePath"></param>
public static string GetSQLScript(string strFilepath,string strNodePath)
{
string strSql;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(strFilepath);
XmlNode node = xmldoc.SelectSingleNode(strNodePath);
strSql = node.ChildNodes[0].InnerText;
return strSql;
}
}
#endregion
}
相关推荐
在本场景中,我们关注的是一个名为"C#中oracle连接数据库的封装类"的主题,这个主题涉及到创建一个名为`DbHelperOra.cs`的类,用于简化与Oracle数据库的交互。这种封装可以提高代码的可读性和复用性,减少直接处理...
这个标题暗示了我们正在讨论的是一个使用C#编程语言开发的体检管理系统。C#是一种面向对象的、现代化的编程语言,广泛应用于构建Windows桌面应用、Web应用以及游戏开发。在这个经典项目中,开发者可能充分利用了C#的...
本文将深入探讨如何使用C#编写一个数据库访问公共类,以实现对SQL Server、Access和Oracle等不同数据库的通用操作。 首先,我们要创建一个数据库访问的基类,这个类通常包含连接数据库的基本方法,如打开、关闭连接...
在本项目"**C#体检套餐系统**"中,我们聚焦于利用C#的面向对象特性来实现一个体检管理系统的功能。这个系统包括了删除套餐、新建套餐以及添加体检项等核心操作,旨在简化体检服务的管理和流程。 **面向对象编程...
总的来说,"家庭账户管理系统前台代码C#"是一个实践性的学习资源,可以帮助开发者了解和掌握C#在实际项目中的应用,特别是对于那些想要学习财务管理系统开发或是提升C#编程技能的人来说,这是一个很好的案例研究。...
以下是一个使用`async`和`await`实现异步连接SQL Server数据库的基本示例: ```csharp using System; using System.Data.SqlClient; using System.Threading.Tasks; public class DatabaseAccess { private ...
通过解压并运行这些文件,开发者或学习者可以深入理解C#在实际项目中的应用,学习如何构建一个功能完备的会员管理系统。此项目适用于计算机科学与技术专业的学生进行毕业设计,同时也可供有经验的开发者参考和借鉴。...
描述中同样提到的是这个压缩包,暗示了它是一个C#开发者长期工作后的成果结晶,可能包含了大量的实用代码片段、自定义控件、扩展方法以及其他有助于项目开发的组件。多年的积累意味着这些类库经过了时间的考验,可能...
在本案例中,我们关注的是一个基于C#实现的三层架构登录系统,数据库采用了Access。 首先,**表现层(Presentation Layer)**是用户与应用交互的部分。在这个例子中,用户界面可能是包含登录表单的Windows窗体或Web...
总结,C#简单通讯录项目是一个基础但实用的桌面应用开发实例,它涉及到C#编程语言的基本使用、面向对象的设计思想、VS2012的开发环境以及Windows Forms的应用。通过这个项目,开发者可以进一步理解C#的特性和实际...
总的来说,这套C#视频监控系统源代码是一个涵盖了视频采集、处理、存储和显示等多个方面的综合项目,对于学习C#编程、视频处理技术以及数据库操作有很高的参考价值。通过深入研究这些源代码,开发者可以了解如何构建...
一个完整的注册登录系统可能包括多个类,如`UserService`用于处理用户注册、登录和密码重设,`DatabaseAccess`用于与数据库交互,以及`Authentication`类用于处理身份验证。设计良好的架构可以帮助代码保持清晰和...
本项目利用C#编程语言,结合VS2015开发环境,实现了一个远程监控数据采集的后台系统。 C#是一种由微软开发的面向对象的编程语言,它具有丰富的类库和强大的.NET框架支持,适合构建复杂的Windows应用程序和服务。在...
【标题】:“基于C#的聊天系统源码.zip”是一个包含C#编程语言实现的聊天系统完整源代码的压缩文件。这个项目可能适用于学习、研究或作为毕业设计的基础,帮助开发者理解如何构建一个实时通信应用。 【描述】:这个...
在“软件设计与体系结构”的第二次实验中,我们通过C#语言来构建一个火车售票系统,旨在理解和运用设计模式,提升软件的可维护性和可扩展性。本篇文章将深入探讨该系统的关键知识点,以及如何使用C#进行高效编程。 ...
【标题】"MyQQ.rar_源码" 涉及的是一个使用C#编程语言编写的源码项目,目标是创建一个类似企业QQ的软件。这个项目可能包含了实现即时通讯、群组聊天、文件传输、在线状态显示等功能的源代码。 在C#编程中,源码通常...
【标题】"通讯录源代码"揭示了这是一个关于创建通讯录应用的编程项目,主要使用的编程语言是C#,并且涉及到数据库管理,很可能是利用SQL进行数据存储和查询。通讯录是一个常见且实用的程序,它允许用户存储、查找和...
总之,"日常管理工具"是一个使用C#编程语言编写的个人财务管理应用,对于初学者来说,这是一个很好的实践项目,同时也能满足用户对日常财务记录的需求。通过持续改进和添加新功能,它可以变得更加实用和全面。
例如,你可以创建一个名为`DatabaseAccess`的基接口,定义如下方法: ```csharp public interface IDatabaseAccess { void ExecuteNonQuery(string sql); DataTable ExecuteQuery(string sql); object ...