SqlHelper简化版数据库操作类
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
namespace SqlDataProvider
{
#region SqlHelper简化版数据库操作类
public abstract class SqlHelper
{
#region 定义变量
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["Conn_DB"].ConnectionString;
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
#endregion
#region 对连接执行查询并返回受影响的行数
/// <summary>
/// 对连接执行查询并返回受影响的行数
/// </summary>
/// <param name="connectionString">连接串</param>
/// <param name="cmdType">类型</param>
/// <param name="cmdText">存储过程名或sql语句</param>
/// <param name="commandParameters">参数数组</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
#endregion
#region 对连接执行事物查询并返回受影响的行数
/// <summary>
/// 对连接执行事物查询并返回受影响的行数
/// </summary>
/// <param name="trans">事物对象</param>
/// <param name="cmdType">类型</param>
/// <param name="cmdText">存储过程名或sql语句</param>
/// <param name="commandParameters">参数数组</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
#endregion
#region 返回SqlDataReader对象
/// <summary>
/// 返回SqlDataReader对象
/// </summary>
/// <param name="connectionString">连接串</param>
/// <param name="cmdType">类型</param>
/// <param name="cmdText">存储过程名或sql语句</param>
/// <param name="commandParameters">参数数组</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);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//这保证关闭连接
cmd.Parameters.Clear();
return rdr;
}
catch(SqlException ex)
{
conn.Close();
//ApplicationLog.WriteError("执行: " + cmdText + "错误信息为: " + ex.Message.Trim());
throw new Exception(ex.ToString());
}
}
#endregion
#region 执行查询,并返回查询所返回的结果集中第一行的第一列
/// <summary>
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
/// </summary>
/// <param name="connectionString">连接串</param>
/// <param name="cmdType">类型</param>
/// <param name="cmdText">存储过程名或sql语句</param>
/// <param name="commandParameters">参数数组</param>
/// <returns>结果集中第一行的第一列或空引用(如果结果集为空)。</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
#endregion
#region 保留参数数组进入缓存的函数
/// <summary>
/// 保留参数数组进入缓存的函数
/// </summary>
/// <param name="cacheKey">缓存中参数数组的key</param>
/// <param name="commandParameters">参数数租</param>
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
#endregion
#region 获取缓存中的参数数租的函数
/// <summary>
/// 获取缓存中的参数数租的函数
/// </summary>
/// <param name="cacheKey">缓存中参数数组的key</param>
/// <returns>参数数组</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;
}
#endregion
#region 加参数函数
/// <summary>
/// Prepare a command for execution
/// </summary>
/// <param name="cmd">SqlCommand object</param>
/// <param name="conn">SqlConnection object</param>
/// <param name="trans">SqlTransaction object</param>
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
/// <param name="cmdParms">SqlParameters to use in the command</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);
}
}
#endregion
}
#endregion
}