zoukankan      html  css  js  c++  java
  • 简单的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 OperateExcel
    {
        public static class SqlHelper
        {
            public static readonly string conStr = ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString;
     
            public static int ExecuteNoQuery(string sql, CommandType cmdType, params SqlParameter[] pms)
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        cmd.CommandType = cmdType;
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
     
                        con.Open();
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
     
            public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms)
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        cmd.CommandType = cmdType;
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        con.Open();
                        return cmd.ExecuteScalar();
                    }
                }
            }
     
            public static SqlDataReader ExecuteReader(string sql, CommandType cmdType, params SqlParameter[] pms)
            {
                SqlConnection con = new SqlConnection(conStr);
                try
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        cmd.CommandType = cmdType;
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        con.Open();
                        SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        return reader;
                    }
                }
                catch (Exception)
                {
                    con.Dispose();
                    throw;
                }
     
            }
     
            public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms)
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(sql, conStr))
                {
                    DataTable dt = new DataTable();
                    sda.SelectCommand.CommandType = cmdType;
                    if (pms != null)
                    {
                        sda.SelectCommand.Parameters.AddRange(pms);
                    }
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
  • 相关阅读:
    Qt对文件的删除、复制、移动、可执行文件位置
    qt关于窗口关闭触发函数/信号
    QString.toUtf8().data()的问题 & char *转换到QByteArray注意
    Qt中类型之间的转换
    C++中auto和decltype的区别和功能
    Delphi 系统[11]关键字和保留字 goto、label
    Delphi 系统[10]关键字和保留字 with
    Delphi 系统[9]关键字和保留字 for、to、downto、do、while、repeat、until
    Delphi 系统[8]关键字和保留字 if、then、else、case
    Delphi 系统[7]关键字和保留字 begin、end
  • 原文地址:https://www.cnblogs.com/jiayue360/p/3166970.html
Copyright © 2011-2022 走看看