4 Star 2 Fork 0

GodSharp / GodSharp.Data.Common.DbProvider

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

GodSharp.Data.Common.DbProvider

DbProviderFactory Factory libary for .NET Core.

AppVeyor build status NuGet MyGet

Supported netstandard1.3 and netstandard2.0.

Getting Started

  1. Install Nuget Package.

See here.

  1. Add Json Parameters.

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:DbConnectionStrings section is not required,but I strongly recommend you add this.

  1. Load DbProviders and DbConnectionStrings[option].
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
// add json file
.AddJsonFile("db.provider.json");

var config = builder.Build();

// Load DbProviders and DbConnectionStrings
DbProviderManager.LoadConfiguration(config);
  1. Get DbConnectionString.
DbConnectionStringSetting conn = DbConnectionStringManager.ConnectionStrings["sqlite"];
  1. Get DbConnectionFactory.
DbProviderFactory factory = DbProviderFactories.GetFactory(conn.ProviderName);
  1. Use DbProviderFactory create IDbConnection and other.

Create IDbConnection

IDbConnection db = factory.CreateConnection();

Use IDbConnection.

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.

MIT License Copyright (c) 2017 GodSharp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

DbProviderFactory Factory libary for .NET Core. 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/GodSharp/GodSharp.Data.Common.DbProvider.git
git@gitee.com:GodSharp/GodSharp.Data.Common.DbProvider.git
GodSharp
GodSharp.Data.Common.DbProvider
GodSharp.Data.Common.DbProvider
master

搜索帮助