• sqlHelper 帮助类


    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 (conn != null && conn.State != ConnectionState.Closed)
                                conn.Close();
                        }
                        
                    }
                }
            }
            //查询操作,返回查询结果中的第一行第一列的值
            public static object ExecuteScalar(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.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 SqlDataReader ExecuteReader(string sqlText, params SqlParameter[] parameters)
            {
                //SqlDataReader要求,它读取数据的时候有,它独占它的SqlConnection对象,而且SqlConnection必须是Open状态
                SqlConnection conn = new SqlConnection(GetSqlConnectionString());//不要释放连接,因为后面还需要连接打开状态
                SqlCommand cmd = conn.CreateCommand();
                conn.Open();
                cmd.CommandText = sqlText;
                cmd.Parameters.AddRange(parameters);
                //CommandBehavior.CloseConnection当SqlDataReader释放的时候,顺便把SqlConnection对象也释放掉
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
    
    }
  • 相关阅读:
    web服务器iis安全设置
    Log explorer for sql server
    C#将image中的显示的图片转换成二进制
    .Net平台开发的技术规范与实践精华总结
    .net面试题整理
    Javascript 刷新框架及页面的方法总集
    WEB程序代码优化入手的几方面
    用js控制网页播放器
    SQL Server2000中死锁经验总结
    专家谈CSS设计
  • 原文地址:https://www.cnblogs.com/Mzg121584668/p/11543583.html
Copyright © 2020-2023  润新知