feat: Client 增加同时操作多种类型数据库示例

This commit is contained in:
Argo Zhang 2020-03-07 14:57:36 +08:00
parent 5ade081cf2
commit 73ac43e97f
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
3 changed files with 36 additions and 8 deletions

View File

@ -1,4 +1,7 @@
using PetaPoco;
using Bootstrap.Security.Mvc;
using Longbow.Data;
using Microsoft.Extensions.Configuration;
using PetaPoco;
using System;
using System.Collections.Specialized;
@ -21,5 +24,30 @@ namespace Bootstrap.Client.DataAccess
db.ExceptionThrown += (sender, args) => args.Exception.Log(new NameValueCollection() { ["LastCmd"] = db.LastCommand });
return db;
}
/// <summary>
/// 创建 Sqlite 类型的 IDatabase 实例
/// </summary>
/// <param name="connectionName">配置文件中配置的数据库连接字符串名称</param>
/// <param name="keepAlive">是否保持连接,默认为 false</param>
/// <returns></returns>
public static IDatabase CreateSqlite(string? connectionName = "client", bool keepAlive = false)
{
// 此方法为演示同时连接不同的数据库操作
// 此处注释为获取连接字符串的不同方法
//var conn = Bootstrap.Security.Mvc.BootstrapAppContext.Configuration["ConnectionStrings:client"];
//var conn = Bootstrap.Security.Mvc.BootstrapAppContext.Configuration.GetSection("ConnectionStrings").GetValue("client", "");
var conn = BootstrapAppContext.Configuration.GetConnectionString(connectionName);
var db = Longbow.Data.DbManager.Create(new DatabaseOption()
{
ProviderName = DatabaseProviderType.SQLite,
ConnectionString = conn,
KeepAlive = keepAlive
});
db.ExceptionThrown += (sender, args) => args.Exception.Log(new NameValueCollection() { ["LastCmd"] = db.LastCommand });
return db;
}
}
}

View File

@ -15,6 +15,12 @@
"MaxFileCount": 1
}
},
"AllowOrigins": "http://localhost:50852",
"SimulateUserName": "Admin",
"BootstrapAdminAuthenticationOptions": {
"AuthHost": "http://localhost:50852",
"KeyPath": "..\\..\\admin\\keys"
},
"ConnectionStrings": {
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa",
"client": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa"
@ -48,11 +54,6 @@
}
}
],
"SimulateUserName": "Admin",
"BootstrapAdminAuthenticationOptions": {
"AuthHost": "http://localhost:50852",
"KeyPath": "..\\..\\admin\\keys"
},
"SmtpClient": {
"Host": "smtp.163.com",
"Port": 587,
@ -61,7 +62,6 @@
"From": "honeywell_mes@163.com",
"To": "argo@163.com"
},
"AllowOrigins": "http://localhost:50852",
"LongbowCache": {
"Enabled": true,
"CacheItems": [

View File

@ -14,7 +14,7 @@
},
"ConnectionStrings": {
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa",
"client": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa"
"client": "Data Source=Client.db;"
},
"DB": [
{