在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:GodSharp.Data.Common.DbProvider开源软件地址:https://gitee.com/GodSharp/GodSharp.Data.Common.DbProvider开源软件介绍:GodSharp.Data.Common.DbProviderDbProviderFactory Factory libary for .NET Core. Supported netstandard1.3 and netstandard2.0. Getting Started
See here.
You can add to any json file,and then add this file. Parameters format like below. { "DbConnectionStrings": [ { "name": "mssql", "connectionString": "Data Source=localhost;Initial Catalog=Master;User Id=sa;Password=1234;", "providerName": "System.Data.SqlClient" }, { "name": "mysql", "connectionString": "Data Source=localhost;Initial Catalog=user;User Id=root;Password=1234;", "providerName": "Pomelo.Data.MySql" }, { "name": "sqlite", "connectionString": "Data Source=data.db", "providerName": "Microsoft.Data.Sqlite" }, { "name": "pgsql", "connectionString": "Host=localhost;Database=postgres;Username=postgres;Password=1234;", "providerName": "Npgsql" } ], "DbProviderFactories": [ { "name": "SqlClient Data Provider", "invariant": "System.Data.SqlClient", "description": ".Net Framework Data Provider for SqlServer", "type": "System.Data.SqlClient.SqlClientFactory, System.Data.SqlClient" }, { "name": "MySQL Data Provider by Pomelo", "invariant": "Pomelo.Data.MySql", "description": ".Net Framework Data Provider for MySql", "type": "Pomelo.Data.MySql.MySqlClientFactory, Pomelo.Data.MySql" }, { "name": "SQLite Data Provider", "invariant": "Microsoft.Data.Sqlite", "description": ".Net Framework Data Provider for SQLite", "type": "Microsoft.Data.Sqlite.SqliteFactory, Microsoft.Data.Sqlite" }, { "name": "Npgsql Data Provider", "invariant": "Npgsql", "description": ".Net Framework Data Provider for PostgreSql", "type": "Npgsql.NpgsqlFactory, Npgsql" } ]} Tips:
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())// add json file.AddJsonFile("db.provider.json");var config = builder.Build();// Load DbProviders and DbConnectionStringsDbProviderManager.LoadConfiguration(config);
DbConnectionStringSetting conn = DbConnectionStringManager.ConnectionStrings["sqlite"];
DbProviderFactory factory = DbProviderFactories.GetFactory(conn.ProviderName);
Create IDbConnection db = factory.CreateConnection(); Use db.ConnectionString = conn.ConnectionString;db.Open();var cmd = db.CreateCommand();cmd.CommandText = "select datetime('now');";var dt = cmd.ExecuteScalar();System.Console.WriteLine(dt);db.Close(); You can see the full code in test. |
请发表评论