using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DBHelper
{
public abstract class SqlHelper
{
//数据库连接字符串
public static readonly string LocalConnectionString = ConfigurationManager.ConnectionStrings["LocalConnectionString"].ConnectionString;
//用HashTable缓存参数
private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
/// <summary>
/// 以指定的数据库连接字符串执行不返回结果集的sql命令
/// 没有SqlParameter[]可以不写
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="cmdType">CommandType (stored procedure, text, etc.) 任选其一</param>
/// <param name="cmdText">存储过程名称或者T-SQL语句</param>
/// <param name="commandParameters">用于执行sql命令的SqlParameter数组</param>
/// <returns>一个表示sql命令影响行数的整数</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 result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return result;
}
}
/// <summary>
/// 使用已存在的SqlConnection执行不返回结果集的sql命令
/// 没有SqlParameter[]可以不写
/// </summary>
/// <param name="connection">一个现有的SqlConnection</param>
/// <param name="cmdType">CommandType (stored procedure, text, etc.) 任选其一</param>
/// <param name="cmdText">存储过程名称或者T-SQL语句</param>
/// <param name="commandParameters">用于执行sql命令的SqlParameter数组</param>
/// <returns>一个表示sql命令影响行数的整数</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return result;
}
/// <summary>
/// 使用已存在的SqlTransaction执不返回结果集的sql命令
/// 没有SqlParameter[]可以不写
/// </summary>
/// <param name="trans">一个现有的SqlTransaction</param>
/// <param name="cmdType">CommandType (stored procedure, text, etc.) 任选其一</param>
/// <param name="cmdText">存储过程名称或者T-SQL语句</param>
/// <param name="commandParameters">用于执行sql命令的SqlParameter数组</param>
/// <returns>一个表示sql命令影响行数的整数</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 result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return result;
}
/// <summary>
/// 以指定的数据库连接字符串执行返回结果集的sql命令,没有参数可以不写
/// 没有SqlParameter[]可以不写
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="cmdType">CommandType (stored procedure, text, etc.) 任选其一</param>
/// <param name="cmdText">存储过程名称或者T-SQL语句</param>
/// <param name="commandParameters">用于执行sql命令的SqlParameter数组</param>
/// <returns>一个包含结果的SqlDataReader</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection connection = new SqlConnection(connectionString);
//这里使用try/catch的原因是,如果发生异常,SqlDataReader不存在CommandBehavior.CloseConnection不会工作
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
catch
{
connection.Close();
throw;
}
}
/// <summary>
/// 以指定的数据库连接字符串执行只返回第一行第一列数据的sql命令
/// 没有SqlParameter[]可以不写
/// </summary>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="cmdType">CommandType (stored procedure, text, etc.) 任选其一</param>
/// <param name="cmdText">存储过程名称或者T-SQL语句</param>
/// <param name="commandParameters">用于执行sql命令的SqlParameter数组</param>
/// <returns>An Object 应始终进行强制类型转换</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;
}
}
/// <summary>
/// 使用已存在的SqlConnection执行只返回第一行第一列数据的sql命令
/// 没有SqlParameter[]可以不写
/// </summary>
/// <param name="connection">一个现有的SqlConnection</param>
/// <param name="cmdType">CommandType (stored procedure, text, etc.) 任选其一</param>
/// <param name="cmdText">存储过程名称或者T-SQL语句</param>
/// <param name="commandParameters">用于执行sql命令的SqlParameter数组</param>
/// <returns>An Object 应始终进行强制类型转换</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 在缓存中添加参数数组
/// </summary>
/// <param name="cacheKey">缓存的键(有意义的字符串)</param>
/// <param name="commandParameters">缓存的值(SqlParameter[])</param>
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
paramCache[cacheKey] = commandParameters;
}
/// <summary>
/// 检索缓存的参数
/// </summary>
/// <param name="cacheKey">用于检索缓存的键</param>
/// <returns>匹配的SqlParameter[]</returns>
public static SqlParameter[] GetCachedParameters(string cacheKey)
{
SqlParameter[] cached = (SqlParameter[])paramCache[cacheKey];
if (cached == null)
return null;
SqlParameter[] cloned =new SqlParameter[cached.Length];
for (int i = 0, j = cached.Length; i < j; i++)
{
cloned[i] = (SqlParameter)((ICloneable)cached[i]).Clone();
}
return cloned;
}
/// <summary>
/// 在执行sql命令之前预处理SqlCommand
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <param name="conn">SqlConnection对象</param>
/// <param name="trans">SqlTransaction对象</param>
/// <param name="cmdType">CommandType (stored procedure, text) 任选其一</param>
/// <param name="cmdText">命令字符串 例如: select * from superman</param>
/// <param name="cmdParams">用于执行sql命令的SqlParameter数组</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParams)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParams != null)
{
foreach (SqlParameter param in cmdParams)
cmd.Parameters.Add(param);
}
}
}
}
分享一下我的SqlHelper类,翻译微软petshop
最新推荐文章于 2019-10-28 16:19:40 发布