public class adosql
{
public static readonly string strcnn = ConfigurationManager.ConnectionStrings["UserInfo"].ConnectionString;
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
public static void CacheParameters(string cacheKey, params SqlParameter[] cmdparas)
{
parmCache[cacheKey] = cmdparas;
}
public static SqlParameter[] GetCacheparas(string cachekey)
{
SqlParameter[] cacheParms = (SqlParameter[])parmCache[cachekey];
if (cacheParms == null)
{
return null;
}
SqlParameter[] clonedParms = new SqlParameter[cacheParms.Length];
for (int i = 0; i < cacheParms.Length; ++i)
{
clonedParms[i] = (SqlParameter)((ICloneable)cacheParms[i]).Clone();
}
return clonedParms;
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdtype, string cmdtext, SqlParameter[] cmdparas)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdtext;
cmd.CommandType = cmdtype;
if (trans != null)
cmd.Transaction = trans;
if (cmdparas != null)
{
foreach (SqlParameter parm in cmdparas)
cmd.Parameters.Add(parm);
}
}
//无参数
public static int ExcuteNonQuery(CommandType cmdType, string cmdText)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(strcnn))
{
PrepareCommand(cmd, con, null, cmdType, cmdText, null);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
//包含参数
public static int ExcuteNonQuery(CommandType cmdType, string cmdText,params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(strcnn))
{
PrepareCommand(cmd, con, null, cmdType, cmdText, paras);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
//包含事务处理
public static int ExcuteNonQuery(CommandType cmdType, string cmdText, SqlTransaction trans, params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(strcnn))
{
PrepareCommand(cmd, con, trans, cmdType, cmdText, paras);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
//返回一个sqldatareader对象
public static SqlDataReader ExcuteReader(CommandType cmdType, string cmdText, params SqlParameter[] paras)
{
SqlCommand cmd=new SqlCommand();
SqlConnection con = new SqlConnection(strcnn);
try
{
PrepareCommand(cmd, con, null, cmdType, cmdText, paras);
SqlDataReader sdr = new SqlDataReader();
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return sdr;
}
catch
{
con.Close();
throw;
}
}
//返回一个结果集的sql查询语句或存储过程
public static DataSet GetData(CommandType cmdType, string cmdText, params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
//定义一个名为DBUReturn的内存数据库变量
DataSet ds = new DataSet("DBUReturn");
using (SqlConnection con = new SqlConnection(strcnn))
{
PrepareCommand(cmd, con, null, cmdType, cmdText, paras);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "DBUReturn");
}
return ds;
}
//返回第一条第一列的数据库查询语句
public static object ExcuteScalar(CommandType cmdType, string cmdText)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(strcnn))
{
PrepareCommand(cmd, con, null, cmdType, cmdText, null);
try
{
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
catch
{
con.Close();
throw;
}
}
}
//带参数
public static object ExcuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(strcnn))
{
PrepareCommand(cmd,con,null,cmdType,cmdText,paras);
try
{
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
catch
{
con.Close();
throw;
}
}
}
//含事务
public static object ExcuteScalar(SqlTransaction trans,CommandType cmdType, string cmdText, params SqlParameter[] paras)
{
if (trans == null)
throw new ArgumentException("trans");
if (trans != null && trans.Connection == null)
throw new ArgumentException("the transaction was rollbacked or commited ,please provide an open transaction","trans");
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd,trans.Connection,trans,cmdType,cmdText,paras);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
}