• [网络收集]DBHelper 常用数据库连接类


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

    namespace DAL
    {
        public static class DBHelper
        {

            private static SqlConnection connection;
            public static SqlConnection Connection
            {
                get
                {
                    string connectionString = ConfigurationManager.ConnectionStrings["MyOfficeDBString"].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;
                }
            }


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

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

            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];
            }
            public static string ReturnStringScalar(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                try
                {
                    string result = cmd.ExecuteScalar().ToString();
                    return result;
                }
                catch (Exception e)
                {
                    return "0";
                }
                connection.Close();
            }

        }
    }

    在Web.Config中的配置信息

    <connectionStrings>
       <add name="MyOfficeDBString" connectionString="server=.;database=MyOffice;uid=sa;pwd=123" providerName="System.Data.SqlClient"/>

    </connectionStrings>

  • 相关阅读:
    Vue
    Vue
    Vue
    Vue
    Vue
    kubernetes
    kubernetes
    kubernetes
    django源码bug解决方案
    UNI-APP 桌面LOGO角标设置(ios)
  • 原文地址:https://www.cnblogs.com/lushuicongsheng/p/1876003.html
Copyright © 2020-2023  润新知