sqlhelper function

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();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        IF (!! = null && conn.State Conn = ConnectionState.Closed)
                            conn.Close ();
                    }
                    
                }
            }
        }
        // query, the query results in return to the first row of the first column value
        public static object ExecuteScalar (string SQL, the params the SqlParameter [] Parameters)
        {
            the using (Conn the SqlConnection the SqlConnection new new = (GetSqlConnectionString ()))
            {
                the using (COMM = conn.CreateCommand the SqlCommand ())
                {
                    the 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)
        {
            The using (new new = the SqlDataAdapter the SqlDataAdapter Adapter (SQL, GetSqlConnectionString ()))
            {
                the DataTable the DataTable dt = new new ();
                adapter.SelectCommand.Parameters.AddRange (Parameters);
                adapter.Fill (dt);
                return dt;
            }
        }

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

}

Guess you like

Origin www.cnblogs.com/bedfly/p/12389206.html