• c#连接字符串和DbUtils


    连接字符串

    <connectionStrings>
          <add name="BaseDb" connectionString="server=127.0.0.1;user id=root;password=12345;database=edusoho;charset=utf8"  providerName="MySql.Data.MySqlClient"  />
         <add name="conn" connectionString="Data Source=.;Database=db001;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /><!--windows身份验证连接字符串-->
        <add name="ConnectionStrings" connectionString="Data Source=IP地址;port=端口号;user id=用户名;password=密码;Charset=utf8" providerName="MySql.Data.MySqlClient" /><!--c#账号密码连接MySQL数据库字符串-->
        <add name="ConnectionStrings" connectionString="Data Source=IP地址;User ID=用户名;Password=密码(可为空);Persist Security Info=True;" providerName="System.Data.SqlClient" /><!--c#账号密码连接SqlServer数据库字符串-->
      <add name="ConnectionStrings" connectionString="data source=电脑名;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
    </connectionStrings><!--c#windows连接SqlServer数据库-->

    dapper

     public class DbUtils
    {
        private static object _obj = new object();
        private static IDbConnection _connection = null;
        public static IDbConnection Main
        {
            get
            {
                IDbConnection dbContext = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
                return dbContext;
            }
        }
        public static SQLiteConnection SqliteConnection
        {
            get
            {
                SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
                sb.DataSource = Configs.DbPath;
                sb.Version = 3;
                sb.Password = Configs.DbPassWord;
                SQLiteConnection connection = null;
    
                if (connection == null)
                {
                    lock (_obj)
                    {
                        if (connection == null)
                        {
                            connection = new SQLiteConnection(sb.ToString());
                        }
                    }
                }
                return connection;
            }
        }
        /// <summary>
        /// using写法
        /// </summary>
        public static IDbConnection SqlServerConnection
        {
            get
            {
                IDbConnection connection = null;
                if (connection == null)
                {
                    lock (_obj)
                    {
                        if (connection == null)
                        {
                            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
                        }
                    }
                }
                return connection;
            }
        }
        /// <summary>
        /// 单例
        /// </summary>
        /// <returns></returns>
        public static IDbConnection SqlServerInstance()
        {
            if (_connection == null)
            {
                lock (_obj)
                {
                    if (_connection == null) // double-check
                    {
                        _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
                    }
                }
            }
            return _connection;
        }
    }
    作者:银龙

    -------------------------------------------

    个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!

  • 相关阅读:
    DRF (Django REST framework) 框架介绍(2)
    DRF (Django REST framework) 框架介绍(1)
    Django中的Admin站点
    Django中的模板和表单
    Django数据库
    跨站请求伪造CSRF以及Flask中的解决办法
    Flask中的模板,Jinjia2介绍
    Flask中的上下文和flask-script扩展
    化学品撮合交易系统
    化学品产品查询系统解决方案
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/14993110.html
Copyright © 2020-2023  润新知