zoukankan      html  css  js  c++  java
  • 【数据库】SQL通用方法集结号

    public static class DBHelper
        {
            private static SqlConnection connection;  //定义连接

            public static SqlConnection Connection    //打开连接
            {
                get
                {
                    string connectionString = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
                    if (connection == null)
                    {
                        connection = new SqlConnection(connectionString);
                    }
                    else if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    else if (connection.State == ConnectionState.Broken)
                    {
                        connection.Close();
                        connection.Open();
                    }
                    return connection;
                }
            }

            //不带参数的执行命令
            public static int ExecuteCommand(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                return cmd.ExecuteNonQuery();
                connection.Close();
            }
            //带参数的执行命令
            public static int ExecuteCommand(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql,Connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
                connection.Close();
            }

         //不带参数,执行受影响行数

            public static int GetScalar(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql,Connection);
                return Convert.ToInt32(cmd.ExecuteScalar());
                connection.Close();
            }

         //带参数,执行受影响的行数

            public static int GetScalar(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                return Convert.ToInt32(cmd.ExecuteScalar());
                connection.Close();
            }

        //不带参数,向前式读取数据库

            public static SqlDataReader GetReader(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
                connection.Close();
            }

         //带参数,向前式读取数据库

            public static SqlDataReader GetReader(string sql,params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
                connection.Close();
            }

            public static DataTable GetDataSet(string sql)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql,Connection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }

            public static DataTable GetDataSet(string sql,params SqlParameter[] values)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }

        }
    }

  • 相关阅读:
    mysql修改密码策略
    YUM方法安装mysql5.7版本
    redis-5.0.5安装(linux centos)
    centos7 安装php7扩展
    Linux df
    Spotlight监控工具使用
    Linux 安装iostat命令
    转载:数据库连接池到底应该设置多大?
    cqlsh 一个错误
    Linux Top命令详解
  • 原文地址:https://www.cnblogs.com/baiboy/p/SQL.html
Copyright © 2011-2022 走看看