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;
        }
    }
}
posted @ 2018-10-29 09:58  吃老干妈就西瓜  阅读(164)  评论(0编辑  收藏  举报