`
weizhai12
  • 浏览: 146085 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

[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
}


分享到:
评论

相关推荐

    JAVA WEB框架,java网站一个模块只用写一个文件

    JAVA WEB框架,java网站一个模块只用写一个文件 以前的servlet在现在的开发中已经不怎么常见,因为操作起来比较原始和麻烦。有些人就是不安于现状去改造它。 做得好的有Struts,Hybernate,Spring那么这些框架都是很...

    图书管理系统源代码

    这是一个比较复杂的数据库 包含图书管理 借书还书 学生管理 老师管理 和数据连接的相关应用 代码有详细的解释 压缩包里面也有 数据库的文件 代码里设置的数据库 用户是 sa 密码是 123456 请使用的时候做相关的修改 ...

    DatabaseAccess:一种设计为易于重用的系统,可为统一的外观提供简单的接口,并具有简单的接口,可连接多种基础数据库实现范例(noSQL,Relational)和技术

    数据库访问 一种旨在易于重用的系统,可为统一的外观提供简单的界面,并可以将其集成到多个基础数据库实现范例(noSQL,Relational)和技术中。

    PassVault:密码记忆申请

    要创建一个新活动,请不要命名它(主要,上层,登录名)。 将其命名为有意义的示例,例如:LoginRequest,PasswordList,DatabaseAccess等(不带下划线“ _”,不带连字符“-”,不带空格“”) 布局文件(.xml)的...

    基于EasyX的贪吃蛇小游戏 - C语言

    基于EasyX的贪吃蛇小游戏 - C语言

    Energy Core ECP5705-V01.pdf

    Energy Core ECP5705-V01.pdf

    matlabGUI学生成绩管理系统pdf

    建立基于图形用户界面GUI的学生成绩管理系统,该系统能够实现学生成绩信息的增加、删除、查询(查询某门课所有学生的成绩并显示排名,查询某个学生的各科成绩并显示排名)、课程成绩统计最高分、最低分、平均分、方差、并显示相应的排名;绘制柱状图、条形图、饼状图、正太分布曲线等功能。 通过本实验使学生掌握图形用户界面GUI的操作和设计流程,并通过编写回调函数巩固前期的知识。

    高职教育品牌专业申报汇总表.doc

    高职教育品牌专业申报汇总表.doc

    游戏运营数据后台需求表.docx

    游戏运营数据后台需求表.docx

    国家开放大学数据库应用技术第三次形考作业3

    使用TOP和CASE的查询。写出实现如下查询的SQL语句。  (18) 列出“数据库基础”课程考试成绩前三名的学生的学号、姓名、所在系和考试成绩。  (19) 查询Java考试成绩最低的学生的姓名、所在系和Java成绩。  (20) 查询选修了Java的学生学号、姓名、所在系和成绩,并对所在系进行如下处理:   当所在系为“计算机系”时,显示“CS”;   当所在系为“信息管理系”时,显示“IS”;   当所在系为“通信工程系”时,显示“CO”;   对其他系,均显示“OTHER”。

    stable diffusion提示词-人物系列

    stable diffusion提示词人物系列,包含提示词和预览图,把提示词复制到stable diffusion里,即可使用。

    mobile-armeabi-v7a-release.apk

    mobile-armeabi-v7a-release.apk

    《计算机网络实验》资料(3).rar

    《计算机网络实验》资料(3).rar

    2024-2030中国HiPOT电气安全测试仪市场现状研究分析与发展前景预测报告 Sample.pdf

    QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。 邮箱:market@qyresearch.com

    python 验证码 高精准 OCR模型 源代码

    OCR模型 源代码,解决网站验证码识别问题,内容包含项目工程源代码,python技术开发,源代码供大家共享

    基于stm32的毕业设计

    基于stm32的毕业设计

    jsp+sql操作系统教学网站设计(lw+程序).zip

    通过操作系统教学网站的建设,完成了对于操作系统课程的远程化授课。可以使学生不受时间空间的限制,通过网络对于这门课程进行学习。建立起了基于B/C的网络化教学系统。本网站采用当前最流行的JSP网络编程技术,可以实现数据的高效、动态、交互访问,具有强大的Server/Client交互能力。本文中所做的主要工作:介绍Win2000 +JSP(J2DK+TOMCAT)系统并且嵌入 JAVABEAN的一般原理;阐述整个操作系统教学网站的概要设计,系统结构及工作原理;分析了系统实现中的特殊性、难点和重点;详细设计实现学院介绍、教学资源、课程表、课堂教学、在线答疑、其他课程、课件下载、留言反馈、站内搜索、公告专栏、友情链接、校园风景、新闻中心、栏目导航等程序模块; 各个模块的具体实现,且分析并解决实现中的若干技术问题;建立完整的实验网站,进行测试并分析结果。 关键字 : JAVABEAN JSP 网络教学 JAVASCRIPT JDBC

    NX二次开发uf5341 函数介绍

    NX二次开发uf5341 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE 分析等领域的专业人士,还是希望提高工作效率的普通用户,NX 二次开发 Ufun 都可以帮助您实现更高效的工作流程。函数覆盖了 NX 软件的各个方面,包括但不限于建模、装配、制图、编程、仿真等。这些 API 函数可以帮助用户轻松地实现自动化、定制化和扩展 NX 软件的功能。例如,用户可以通过 Ufun 编写脚本,自动化完成重复性的设计任务,提高设计效率;或者开发定制化的功能,满足特定的业务需求。语法简单易懂,易于学习和使用。用户可以快速上手并开发出符合自己需求的 NX 功能。本资源内容 提供了丰富的中英文帮助文档,可以帮助用户快速了解和使用 Ufun 的功能。用户可以通过资源中的提示,学习如何使用 Ufun 的 API 函数,以及如何实现特定的功能。

    医保信息平台定点医药机构国家标准接口技术文档V5.2

    医保信息平台定点医药机构国家标准接口技术文档V5.2 医保平台接口文档,开发必备,全语言接口

    VB医疗纠纷检索系统设计(源代码+系统)【VB】.zip

    VB医疗纠纷检索系统设计(源代码+系统)【VB】

Global site tag (gtag.js) - Google Analytics