sqlHelper helper

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UpdateUserId_ConsoleApp
{
    class sqlHelper
    {
        public static string GetSqlConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["sqlConnection"].ToString();
        }
        //适合增删改操作,返回影响条数
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(GetSqlConnectionString()))
            {
                using (SqlCommand comm = conn.CreateCommand())
                {
                    try
                    {
                        conn.Open();
                        comm.CommandText = sql;
                        comm.Parameters.AddRange(parameters);
                        return comm.ExecuteNonQuery();
                    }
                    the catch (Exception EX) 
                    { 
                        the throw  new new Exception (ex.Message); 
                    } 
                    the finally 
                    { 
                        IF (Conn =! null && conn.State =! ConnectionState.Closed) 
                            conn.Close (); 
                    } 
                    
                } 
            } 
        } 
        // query, returns the first row in the query result value of the first column 
        public  static  Object the ExecuteScalar ( String SQL, the params the SqlParameter [] Parameters) 
        { 
            the using (the SqlConnection Conn = new new SqlConnection(GetSqlConnectionString()))
            {
                using (SqlCommand comm = conn.CreateCommand())
                {
                    try
                    {
                        conn.Open();
                        comm.CommandText = sql;
                        comm.Parameters.AddRange(parameters);
                        return comm.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        if (conn != null && conn.State != ConnectionState.Closed)
                            conn.Close();
                    }
                }
            }
        }
        //Adapter调整,查询操作,返回DataTable
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, GetSqlConnectionString()))
            {
                DataTable dt = new DataTable();
                adapter.SelectCommand.Parameters.AddRange(parameters);
                adapter.Fill (dt); 
                return  dt;
            } 
        } 

        public  static the SqlDataReader the ExecuteReader ( String sqlText, the params the SqlParameter [] Parameters) 
        { 
            // the SqlDataReader requirements, when it has read data, it its exclusive SqlConnection object, and must be Open SqlConnection state of 
            the SqlConnection Conn = new new the SqlConnection (GetSqlConnectionString ()); // Do not release the connection, since the latter also need to connect open 
            the SqlCommand cmd = conn.CreateCommand (); 
            conn.Open (); 
            cmd.CommandText = sqlText; 
            cmd.Parameters. AddRange (the Parameters); 
            //CommandBehavior.CloseConnection SqlDataReader when released, the way the objects are also freed SqlConnection 
            return cmd.ExecuteReader (CommandBehavior.CloseConnection); 
        } 
    } 

}

 

Guess you like

Origin www.cnblogs.com/Mzg121584668/p/11543583.html