https://github.com/godsharp/GodSharp.Data.Common.DbProvider
DbProviderFactory Factory libary for .NET Core.
https://github.com/godsharp/GodSharp.Data.Common.DbProvider
dbconnectionstrings dbprovider dbproviderfactories dbproviderfactory
Last synced: 25 days ago
JSON representation
DbProviderFactory Factory libary for .NET Core.
- Host: GitHub
- URL: https://github.com/godsharp/GodSharp.Data.Common.DbProvider
- Owner: godsharp
- License: mit
- Created: 2017-09-14T06:48:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-09-14T12:22:25.000Z (over 7 years ago)
- Last Synced: 2024-12-04T19:06:00.811Z (6 months ago)
- Topics: dbconnectionstrings, dbprovider, dbproviderfactories, dbproviderfactory
- Language: C#
- Homepage:
- Size: 10.7 KB
- Stars: 9
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GodSharp.Data.Common.DbProvider
DbProviderFactory Factory libary for .NET Core.[](https://ci.appveyor.com/project/seayxu/godsharp-data-common-dbprovider/) [](https://www.nuget.org/packages/GodSharp.Data.Common.DbProvider/) [](https://www.myget.org/Package/Details/seay?packageType=nuget&packageId=GodSharp.Data.Common.DbProvider)
Supported netstandard1.3 and netstandard2.0.
# Getting Started
1. Install Nuget Package.
See [here](https://www.nuget.org/packages/GodSharp.Data.Common.DbProvider/).
2. Add Json Parameters.
You can add to any json file,and then add this file.
Parameters format like below.
``` json
{
"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.*
3. 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);
```4. Get DbConnectionString.
```
DbConnectionStringSetting conn = DbConnectionStringManager.ConnectionStrings["sqlite"];
```5. Get DbConnectionFactory.
```
DbProviderFactory factory = DbProviderFactories.GetFactory(conn.ProviderName);
```6. 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.