叶祖辉

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
复制代码
   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);
                }
            }
      }
复制代码
posted on 2017-05-11 21:02  叶祖辉  阅读(158)  评论(0编辑  收藏  举报
友情链接:回力鞋官网 | 中老年女装 | 新航道托福 | 十月英文 | 电影推荐