公共的数据库访问访问类 SqlHelper.cs
时间:2021-07-01 10:21:17
帮助过:24人阅读
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace DotNet.Utilities
{
public abstract class SqlHelper
{
public static readonly string connectionString = System.Configuration.ConfigurationSettings.AppSettings["con"].ToString().Trim();
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
#region //ExecteNonQuery方法
public static int ExecteNonQuery(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;
}
}
public static int ExecteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
return ExecteNonQuery(connectionString, cmdType, cmdText, commandParameters);
}
public static int ExecteNonQueryProducts(string cmdText, params SqlParameter[] commandParameters)
{
return ExecteNonQuery(CommandType.StoredProcedure, cmdText, commandParameters);
}
public static int ExecteNonQueryText(string cmdText, params SqlParameter[] commandParameters)
{
return ExecteNonQuery(CommandType.Text, cmdText, commandParameters);
}
#endregion
#region//GetTable方法
public static DataTableCollection GetTable(string connecttionString, CommandType cmdTye, string cmdText, SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connecttionString))
{
PrepareCommand(cmd, conn, null, cmdTye, cmdText, commandParameters);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
}
DataTableCollection table = ds.Tables;
return table;
}
public static DataTableCollection GetTable(CommandType cmdTye, string cmdText, SqlParameter[] commandParameters)
{
return GetTable(SqlHelper.connectionString, cmdTye, cmdText, commandParameters);
}
public static DataTableCollection GetTableProducts(string cmdText, SqlParameter[] commandParameters)
{
return GetTable(CommandType.StoredProcedure, cmdText, commandParameters);
}
public static DataTableCollection GetTableText(string cmdText, SqlParameter[] commandParameters)
{
return GetTable(CommandType.Text, cmdText, commandParameters);
}
#endregion
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);
}
}
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;
}
}
#region//ExecuteDataSet方法
public static DataSet ExecuteDataSet(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);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
return ds;
}
}
catch
{
throw;
}
}
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
return ExecuteDataSet(connectionString, cmdType, cmdText, commandParameters);
}
public static DataSet ExecuteDataSetProducts(string cmdText, params SqlParameter[] commandParameters)
{
return ExecuteDataSet(connectionString, CommandType.StoredProcedure, cmdText, commandParameters);
}
public static DataSet ExecuteDataSetText(string cmdText, params SqlParameter[] commandParameters)
{
return ExecuteDataSet(connectionString, CommandType.Text, cmdText, commandParameters);
}
public static DataView ExecuteDataSet(string connectionString, string sortExpression, string direction, 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);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
dv.Sort = sortExpression + " " + direction;
return dv;
}
}
catch
{
throw;
}
}
#endregion
#region // ExecuteScalar方法
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
return ExecuteScalar(SqlHelper.connectionString, cmdType, cmdText, commandParameters);
}
public static object ExecuteScalarProducts(string cmdText, params SqlParameter[] commandParameters)
{
return ExecuteScalar(SqlHelper.connectionString, CommandType.StoredProcedure, cmdText, commandParameters);
}
public static object ExecuteScalarText(string cmdText, params SqlParameter[] commandParameters)
{
return ExecuteScalar(SqlHelper.connectionString, CommandType.Text, cmdText, commandParameters);
}
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;
}
}
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
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
public static SqlParameter[] GetCachedParameters(string cacheKey)
{
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return n