码迷,mamicode.com
首页 > 数据库 > 详细

【ADO.NET】7、SQL高级封装

时间:2017-05-11 22:31:21      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:gif   lcm   过程   each   ado   store   comm   query   类型   

技术分享
   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);
                }
            }
      }
技术分享

【ADO.NET】7、SQL高级封装

标签:gif   lcm   过程   each   ado   store   comm   query   类型   

原文地址:http://www.cnblogs.com/yezuhui/p/6842759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!