zoukankan      html  css  js  c++  java
  • asp.net 数据库连接类 (一)

    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;
    }

    }
    }

  • 相关阅读:
    RabbitMQ官方文档翻译之Simple(一)
    rabbitMq集成Spring后,消费者设置手动ack,并且在业务上控制是否ack
    RabbitMQ消息队列知识点归纳
    理解Java中HashMap的工作原理
    mybatis 主键回显
    quart任务调度框架实战
    springmvc常用注解标签详解
    Java程序员玩Linux学操作系统
    在网页中发起QQ临时对话的方法
    软件测试技术学习总结
  • 原文地址:https://www.cnblogs.com/itmangelihai/p/2509727.html
Copyright © 2011-2022 走看看