using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
namespace SuppliesManagement.Common
{
/// <summary>
/// SqlHelper类(封装SqlHelper高性能的、可扩展的最佳做法)
/// </summary>
public abstract class SqlHelper
{
//数据库连接字符串
public static readonly string ConnectionString_SuppliesManagement = ConfigurationManager.ConnectionStrings["SuppliesManagementConnectionString"].ConnectionString;
public static readonly string ConnectionString_BTC_Common = ConfigurationManager.ConnectionStrings["BTC_CommonConnectionString"].ConnectionString;
// Hashtable储存缓存参数
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
/// <summary>
/// 对现有的数据库连接使用提供的参数执行一个SqlCommand(即没有返回结果集)
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的连接字符串为SqlConnection </param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">以一个数组的形式返回SqlParamters用来执行命令</param>
/// <returns>返回一个int值,表示受该命令影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 对现有的数据库连接使用提供的参数执行一个SqlCommand(即没有返回结果集)
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">现有的数据库连接</param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text)等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个int值,表示受该命令影响的行数</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行一个SqlCommand(即没有返回结果集)使用提供的参数使用现有的SQL事务。
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">现有的数据库连接</param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个int值,表示受该命令影响的行数</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行一个SqlCommand,反对使用提供的参数在连接字符串中指定的数据库返回的结果集。
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">现有的数据库连接</param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个SqlDataReader包含的结果</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行带参数的SQL语句返回一个DataSet
/// </summary>
/// <param name="connectionString">现有的数据库连接</param>
/// <param name="cmdType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="cmdText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个DataSet集合</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
DataSet ds = new DataSet();
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataAdapter dapter = new SqlDataAdapter(cmd);
dapter.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
catch (Exception ex)
{
conn.Close();
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行反对在连接字符串中指定的数据库返回的第一条记录的第一列的SqlCommand使用提供的参数。
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">现有的数据库连接</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个对象,应该被转换为预期的类型使用Convert.To {类型}</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行一个SqlCommand,对现有的数据库连接使用提供的参数返回的第一条记录的第一列。
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">现有的数据库连接</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个对象,应该被转换为预期的类型使用Convert.To {类型}</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 参数数组添加到缓存
/// </summary>
/// <param name="cacheKey">关键参数缓存</param>
/// <param name="cmdParms">以一个数组的形式返回SqlParamters要缓存 </param>
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// 检索缓存参数
/// </summary>
/// <param name="cacheKey">用来查找关键参数</param>
/// <returns>缓存SqlParamters阵列</returns>
public static SqlParameter[] GetCachedParameters(string cacheKey)
{
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
/// <summary>
/// 准备执行命令
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <param name="conn">SqlConnection对象</param>
/// <param name="trans">SqlTransaction对象</param>
/// <param name="cmdType">Cmd的类型,例如存储过程或文本</param>
/// <param name="cmdText">命令文本, e.g. Select * from Products</param>
/// <param name="cmdParms">SqlParameters使用命令</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
namespace SuppliesManagement.Common
{
/// <summary>
/// SqlHelper类(封装SqlHelper高性能的、可扩展的最佳做法)
/// </summary>
public abstract class SqlHelper
{
//数据库连接字符串
public static readonly string ConnectionString_SuppliesManagement = ConfigurationManager.ConnectionStrings["SuppliesManagementConnectionString"].ConnectionString;
public static readonly string ConnectionString_BTC_Common = ConfigurationManager.ConnectionStrings["BTC_CommonConnectionString"].ConnectionString;
// Hashtable储存缓存参数
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
/// <summary>
/// 对现有的数据库连接使用提供的参数执行一个SqlCommand(即没有返回结果集)
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的连接字符串为SqlConnection </param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">以一个数组的形式返回SqlParamters用来执行命令</param>
/// <returns>返回一个int值,表示受该命令影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 对现有的数据库连接使用提供的参数执行一个SqlCommand(即没有返回结果集)
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">现有的数据库连接</param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text)等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个int值,表示受该命令影响的行数</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行一个SqlCommand(即没有返回结果集)使用提供的参数使用现有的SQL事务。
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">现有的数据库连接</param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个int值,表示受该命令影响的行数</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行一个SqlCommand,反对使用提供的参数在连接字符串中指定的数据库返回的结果集。
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">现有的数据库连接</param>
/// <param name="commandType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个SqlDataReader包含的结果</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行带参数的SQL语句返回一个DataSet
/// </summary>
/// <param name="connectionString">现有的数据库连接</param>
/// <param name="cmdType">CommandType(存储过程(stored procedure),文本(text),等等)</param>
/// <param name="cmdText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个DataSet集合</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
DataSet ds = new DataSet();
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataAdapter dapter = new SqlDataAdapter(cmd);
dapter.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
catch (Exception ex)
{
conn.Close();
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行反对在连接字符串中指定的数据库返回的第一条记录的第一列的SqlCommand使用提供的参数。
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">现有的数据库连接</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个对象,应该被转换为预期的类型使用Convert.To {类型}</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 执行一个SqlCommand,对现有的数据库连接使用提供的参数返回的第一条记录的第一列。
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">现有的数据库连接</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">存储过程名称或T-SQL命令</param>
/// <param name="commandParameters">一个用于执行命令的SqlParamters数组</param>
/// <returns>返回一个对象,应该被转换为预期的类型使用Convert.To {类型}</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
catch (Exception ex)
{
Common.PublicFunction.ErrorLogPrint(cmdText, ex.Message);
throw;
}
}
/// <summary>
/// 参数数组添加到缓存
/// </summary>
/// <param name="cacheKey">关键参数缓存</param>
/// <param name="cmdParms">以一个数组的形式返回SqlParamters要缓存 </param>
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// 检索缓存参数
/// </summary>
/// <param name="cacheKey">用来查找关键参数</param>
/// <returns>缓存SqlParamters阵列</returns>
public static SqlParameter[] GetCachedParameters(string cacheKey)
{
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
/// <summary>
/// 准备执行命令
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <param name="conn">SqlConnection对象</param>
/// <param name="trans">SqlTransaction对象</param>
/// <param name="cmdType">Cmd的类型,例如存储过程或文本</param>
/// <param name="cmdText">命令文本, e.g. Select * from Products</param>
/// <param name="cmdParms">SqlParameters使用命令</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}