zoukankan      html  css  js  c++  java
  • sqlhelper帮助类

      public class SQLHelperNew
        {
    
            //连接字符串
            private static readonly string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    
    
    
            /// <summary>
            /// 返回SqlDataReader
            /// </summary>
            /// <param name="sql">连接字符串</param>
            /// <param name="cmdParms">CMD参数集合</param>
            /// <returns></returns>
            public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] cmdParms)
            {
                using (SqlConnection con = new SqlConnection(ConnStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        if (cmdParms.Count() > 0)
                        {
                            cmd.Parameters.AddRange(cmdParms);
                        }
                        try
                        {
                            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            throw e;
                        }
                        finally
                        {
                            con.Close();
                            cmd.Dispose();
                        }
                    }
                }
            }
    
            /// <summary>
            /// 返回数据表
            /// </summary>
            /// <param name="sql">连接字符串</param>
            /// <param name="cmdParms">CMD参数集合</param>
            /// <returns></returns>
            public static DataTable ExecuteDataTable(string sql, params SqlParameter[] cmdParms)
            {
                using (SqlConnection con = new SqlConnection(ConnStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        DataTable dt = new DataTable();
                        if (cmdParms.Count() > 0)
                        {
                            cmd.Parameters.AddRange(cmdParms);
                        }
                        try
                        {
                            con.Open();
                            SqlDataAdapter rd = new SqlDataAdapter(cmd);
                            rd.Fill(dt);
                            return dt;
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            throw e;
                        }
                        finally
                        {
                            con.Close();
                            cmd.Dispose();
                        }
                    }
                }
            }
    
            /// <summary>
            /// 返回受影响的行数
            /// </summary>
            /// <param name="sql">连接字符串</param>
            /// <param name="cmdParms">CMD参数集合</param>
            /// <returns></returns>
            public static int ExecuteNonQuery(string sql, params SqlParameter[] cmdParms)
            {
                using (SqlConnection con = new SqlConnection(ConnStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        if (cmdParms.Count() > 0)
                        {
                            cmd.Parameters.AddRange(cmdParms);
                        }
                        try
                        {
                            return cmd.ExecuteNonQuery();
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            throw e;
                        }
                        finally
                        {
                            con.Close();
                            cmd.Dispose();
                        }
                    }
                }
            }
    
            /// <summary>
            /// 返回首行首列
            /// </summary>
            /// <param name="sql">连接字符串</param>
            /// <param name="cmdParms">CMD参数集合</param>
            /// <returns></returns>
            public static object ExecuteScalar(string sql, params SqlParameter[] cmdParms)
            {
                using (SqlConnection con = new SqlConnection(ConnStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        if (cmdParms.Count() > 0)
                        {
                            cmd.Parameters.AddRange(cmdParms);
                        }
                        try
                        {
                            return cmd.ExecuteScalar();
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            throw e;
                        }
                        finally
                        {
                            con.Close();
                            cmd.Dispose();
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    [C#] 等待启动的进程执行完毕
    C# 、winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示
    Mysql 备份
    Mysql 慢查询日志配置
    Mysql 忘记密码处理配置
    PHP-FPM 慢执行日志、网站隔离配置
    PHP-FPM 设置多pool、配置文件重写
    Nginx 代理配置
    Nginx 301与302配置
    Nginx URL跳转
  • 原文地址:https://www.cnblogs.com/demoC/p/5121270.html
Copyright © 2011-2022 走看看