dbhelp
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace DBUtils
{
/// <summary>
/// 基于数据库SQL SERVER的常规数据库操作
/// 本类修改自Mirosoft .NET Pet Shop 4.0 的数据库操作通用类
/// 修改:icefire @ 20070213
/// </summary>
public abstract class SqlHelper
{
// 哈希表保存数据库参数
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
#region 常规数据库操作
#region 执行非查询语句
/// <summary>
/// 通过数据库字符串链接执行非查询语句,如INSERT,UPDATE,DEL等操作
/// </summary>
/// <remarks>
/// 例如:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">数据库链接字符串</param>
/// <param name="cmdType">SQL命令类型,如存储过程,文本语句等</param>
/// <param name="cmdText">SQL命令字符串</param>
/// <param name="commandParameters">SQL参数数组</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;
}
}
/// <summary>
/// 通过已经存在的数据库链接执行非查询语句,如INSERT,UPDATE,DEL等操作
/// </summary>
/// <remarks>
/// 例如
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connection">数据库链接对象</param>
/// <param name="cmdType">SQL命令类型,如存储过程,文本语句等</param>
/// <param name="cmdText">SQL命令字符串</param>
/// <param name="commandParameters">SQL参数数组</param>
/// <returns>返回处理结果</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 val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 以 Transact-SQL 事务方式执行非查询语句
/// </summary>
/// <param name="trans">事务对象</param>
/// <param name="cmdType">SQL命令类型,如存储过程,文本语句等</param>
/// <param name="cmdText">SQL命令字符串</param>
/// <param name="commandParameters">SQL参数数组</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 查询数据库操作并返回结果
/// <summary>
/// 通过数据库链接字符串执行查询命令并返回查询结果
/// </summary>
/// <param name="connectionString">数据库链接字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">SQL参数数组</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
{
conn.Close();
throw;
}
}
#endregion
#region 执行语句返回第一行,第一列的值
/// <summary>
/// 执行查询并返回结果集的第一行的第一列,次执行效率较高
/// </summary>
/// <param name="connectionString">数据库链接字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">SQL参数数组</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;
}
}
/// <summary>
/// 执行查询并返回结果集的第一行的第一列,次执行效率较高
/// </summary>
/// <param name="connection">数据库连接对象</param>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">SQL参数数组</param>
/// <returns>返回结果对象</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;
}
#endregion
#endregion
#region 将查询结果保存到DataSet中
/// <summary>
/// 根据数据库连接字符串执行查询并返回结果DataSet
/// </summary>
/// <param name="connectionString">数据库链接字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">SQL参数数组</param>
/// <returns>返回结果</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet dsReturn = new DataSet();
//准备Command命令
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
//执行命令
da.SelectCommand = cmd;
da.Fill(dsReturn);
cmd.Parameters.Clear();
return dsReturn;
}
}
/// <summary>
/// 根据数据库连接执行查询并返回结果DataSet
/// </summary>
/// <param name="connection">数据库连接对象</param>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">SQL参数数组</param>
/// <returns>返回结果</returns>
public static DataSet ExecuteDataSet(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataSet dsReturn = new DataSet();
//准备Command命令
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
//执行命令
da.SelectCommand = cmd;
da.Fill(dsReturn);
cmd.Parameters.Clear();
return dsReturn;
}
#endregion
#region 将查询结果保存到DataTable中
/// <summary>
/// 根据数据库连接字符串执行查询并返回结果DataTable
/// </summary>
/// <param name="connectionString">数据库链接字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">SQL参数数组</param>
/// <returns>返回结果</returns>
public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dtReturn = new DataTable();
//准备Command命令
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
//执行命令
da.SelectCommand = cmd;
da.Fill(dtReturn);
cmd.Parameters.Clear();
return dtReturn;
}
}
/// <summary>
/// 根据数据库连接执行查询并返回结果DataTable
/// </summary>
/// <param name="connection">数据库连接对象</param>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">SQL参数数组</param>
/// <returns>返回结果</returns>
public static DataTable ExecuteDataTable(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dtReturn = new DataTable();
//准备Command命令
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
//执行命令
da.SelectCommand = cmd;
da.Fill(dtReturn);
cmd.Parameters.Clear();
return dtReturn;
}
#endregion
#region 数据库命令数组缓存操作
/// <summary>
/// 增加数据库超作参数到缓存
/// </summary>
/// <param name="cacheKey">缓存健</param>
/// <param name="cmdParms">被缓存的SQL参数数组</param>
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// 根据缓存健得到缓存的命令数组
/// </summary>
/// <param name="cacheKey">缓存健</param>
/// <returns>被缓存的SQL参数数组</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 对SQL命令预处理
/// <summary>
/// 准备一个执行命令
/// </summary>
/// <param name="cmd">数据库命令字对象</param>
/// <param name="conn">数据库链接对象</param>
/// <param name="trans">数据库事务对象</param>
/// <param name="cmdType">SQL命令类型,如存储过程,文本语句等</param>
/// <param name="cmdText">SQL命令字符串</param>
/// <param name="cmdParms">SQL参数数组</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
}
}
作者:过错
出处:http://www.cnblogs.com/wang2650/
关于作者:net开发做的久而已。十余年时光虚度!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:wang2650@163.com
联系我,非常感谢。