zoukankan      html  css  js  c++  java
  • DBHelper

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace MyBookShop.DAL
    {
        public static class DBHelper
        {

            private static SqlConnection connection;
            public static SqlConnection Connection
            {
                get 
                {
                    string connectionString = ConfigurationManager.ConnectionStrings["MyBookShop"].ConnectionString;
                    if (connection == null)
                    {
                        connection = new SqlConnection(connectionString);
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Broken)
                    {
                        connection.Close();
                        connection.Open();
                    }
                    return connection;
                }
            }

            //ExecuteNonQuery方法是用来执行insert、delete、update语句的,返回的是影响的行数
            public static int ExecuteCommand(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                int result = cmd.ExecuteNonQuery();
                return result;
            }

            public static int ExecuteCommand(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }


            //ExecuteScalar()返回sql语句执行后的第一行第一列的值,object类型
            public static int GetScalar(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }

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

            //ExecuteReader()返回一个Datareader对象,对象内容是为与命令匹配的所有行,通常用于读取数据
            public static SqlDataReader GetReader(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }

            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;
            }

            public static DataTable GetDataSet(string safeSql)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(safeSql, 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];
            }

        }
    }

  • 相关阅读:
    LSB最低有效位和MSB最高有效位
    STM32(十二)通过I2C总线向EEPROM总线中读写数据的过程
    EEPROM和Flash的区别
    Prometheus监控系统的从无到有
    shell 提取第一个指定参数前面的内容
    myBase7试用到期 破解步骤
    docker入门(六)端口映射于容器关联
    docker入门(五)docker数据管理
    针对ansible的一次调研
    docker入门(四)访问docker仓库
  • 原文地址:https://www.cnblogs.com/top5/p/1724722.html
Copyright © 2011-2022 走看看