zoukankan      html  css  js  c++  java
  • 【ADO.NET】7、SQL高级封装

    这次是更加简化的进行封装,所有的cmd操作命令都封装到了 Allcmd() 方法里面
    别外还有一个别点是 每次执行命令完后,都会垃圾回收, cmd.Parameters.Clear();
    是先将执行返回的结果用变量存储,然后在 Clear 掉,然后再将变量值返回出去

       public static string str = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
        
        /// <summary>
            /// 用于查询数据返回 SqlDataReader 类型
            /// </summary>
            /// <param name="ConnectionStrs">连接字符串</param>
            /// <param name="type">操作类型(text/StoredProcedure)</param>
            /// <param name="cmdtext">操作命令(存储过程)</param>
            /// <param name="parameter">参数</param>
            /// <returns></returns>
       public static SqlDataReader ExecuteReader(string ConnectionStrs,CommandType type,string cmdtext, params SqlParameter[] parameter)
         {
                SqlConnection conn = new SqlConnection(ConnectionStrs);
                SqlCommand cmd = conn.CreateCommand();
                Allcmd(conn, cmd, type, cmdtext, parameter);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return dr;
          }
    
            /// <summary>
            /// 用于更新,删除,插入数据返回int类型
            /// </summary>
            /// <param name="ConnectionStrs">连接字符串</param>
            /// <param name="type">操作类型(text/StoredProcedure)</param>
            /// <param name="cmdtext">操作命令(存储过程)</param>
            /// <param name="parameter">参数</param>
            /// <returns></returns>
       public static int ExecuteNoQuery(string ConnectionStrs, CommandType type, string cmdtext, params SqlParameter[] parameter)
         {
                using (SqlConnection conn = new SqlConnection(ConnectionStrs))
                {
                    SqlCommand cmd = conn.CreateCommand();
                    Allcmd(conn, cmd,type, cmdtext, parameter);
                    int val = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return val;
                }
          }
    
            /// <summary>
            /// 用于返回首行首列的数据返回object类型
            /// </summary>
            /// <param name="ConnectionStrs">连接字符串</param>
            /// <param name="type">操作类型(text/StoredProcedure)</param>
            /// <param name="cmdtext">操作命令(存储过程)</param>
            /// <param name="parameter">参数</param>
            /// <returns></returns>
        public static object ExecuteScalar(string ConnectionStrs, CommandType type, string cmdtext, params SqlParameter[] parameter)
          {
                using (SqlConnection conn = new SqlConnection(ConnectionStrs))
                {
                    SqlCommand cmd = conn.CreateCommand();
                    Allcmd(conn, cmd, type, cmdtext, parameter);
                    object val = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    return val;
                }
            }
    
            /// <summary>
            /// 用于操作全部的连接命令
            /// </summary>
            /// <param name="conn">连接对象</param>
            /// <param name="cmd">命令操作对象</param>
            /// <param name="Type">命令操作类型</param>
            /// <param name="cmdText">执行语句</param>
            /// <param name="cmdParms">参数对象</param>
        private static void Allcmd(SqlConnection conn, SqlCommand cmd, CommandType Type, string cmdText, SqlParameter[] cmdParms)
          {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                cmd.Connection = conn;
                cmd.CommandType = Type;
                cmd.CommandText = cmdText;
                if (cmdParms != null)
                {
                    foreach (SqlParameter par in cmdParms)
                    {
                        cmd.Parameters.Add(par);
                    }
                }
          }
  • 相关阅读:
    JavaScript之数学对象Math
    JavaScript之数据类型转换
    JavaScript之操作符
    JavaScript之基本语句
    JavaScript之基本概念(二)
    JavaScript之基本概念(一)
    使用velero进行kubernetes灾备
    minikube配置CRI-O作为runtime并指定flannel插件
    使用thanos管理Prometheus持久化数据
    linux开启tcp_timestamps和tcp_tw_recycle引发的问题研究
  • 原文地址:https://www.cnblogs.com/xgao/p/4135173.html
Copyright © 2011-2022 走看看