• SqlHelper


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    namespace Demo2017
    {
        public class SqlHelper
        {
            public static SqlDataReader ExecuteReader(string constr, string cmdstr, CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.CommandType = type;
                if (ps.Length > 0)
                {
                    cmd.Parameters.AddRange(ps);
                }
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }

            public static int ExecuteNonQuery(string constr, string cmdstr,CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.CommandType = type;
                if (ps.Length > 0)
                {
                    cmd.Parameters.AddRange(ps);
                }
               int i= cmd.ExecuteNonQuery();
               return i;
            }

            public static DataSet ExecuteDataSet(string constr, string cmdstr,CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
          
                SqlDataAdapter sda = new SqlDataAdapter(cmdstr, conn);      
                sda.SelectCommand.CommandType=type;
                if (ps.Length > 0)
                {
                    sda.SelectCommand.Parameters.AddRange(ps);
                }
                DataSet ds = new DataSet();
                sda.Fill(ds);              
                return ds;
            }

            public static object ExecuteScalar(string constr, string cmdstr,CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.CommandType = type;
                if (ps.Length > 0)
                {
                    cmd.Parameters.AddRange(ps);
                }
                object o = cmd.ExecuteScalar();
                return o;
            }
        }
    }
  • 相关阅读:
    (转)AspNetPager分页控件的使用方法 (1)
    对GridView控件的操作:GridView控件的HyperLinkField超链接数据行
    JS实现在Repeater控件中创建可隐藏区域的代码
    Asp.net弹出窗口大全
    AspNetPager分页控件使用方法 (2)
    解决mapXtreme地图更新闪屏
    绝对路径与相对路径的区别【2】release和debug这两种情况下对本地文件操作不一致的问题
    今天开始添加点文章
    第一篇博客~~
    使用 PIVOT 和 UNPIVOT实现行转列,列转行
  • 原文地址:https://www.cnblogs.com/wantnewlive/p/9868996.html
Copyright © 2020-2023  润新知