Skip to content

Commit

Permalink
读取连接字符串,联合使用 appsettings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 23, 2018
1 parent 860b59c commit 7bf208d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
6 changes: 6 additions & 0 deletions TestST/TestST.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@
<ProjectReference Include="..\XCodeST\XCodeST.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions TestST/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"exclude": [
"**/bin",
"**/bower_components",
"**/jspm_packages",
"**/node_modules",
"**/obj",
"**/platforms"
],
"ConnectionStrings": {
"SQLite": {
"connectionString": "Data Source=test.db;",
"providerName": "Sqlite"
},
"MySql": {
"connectionString": "Server=.;Port=3306;Database=mysql;Uid=root;Pwd=;",
"providerName": "MySql.Data.MySqlClient"
},
"MSSQL": {
"connectionString": "Server=.;User ID=sa;Password=sa;Database=Test;datapath=~\\App_Data",
"providerName": "System.Data.SqlClient"
},
"Oracle": {
"connectionString": "Data Source=Tcp://127.0.0.1/ORC;User ID=sys;Password=admin;Owner=mis",
"providerName": "System.Data.OracleClient"
}
}
}
36 changes: 32 additions & 4 deletions XCode/DataAccessLayer/DAL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NewLife;
using NewLife.Log;
using NewLife.Reflection;
using NewLife.Serialization;
using XCode.Code;

namespace XCode.DataAccessLayer
Expand Down Expand Up @@ -124,6 +125,7 @@ public static Dictionary<String, String> ConnStrs

var file = "web.config".GetFullPath();
var fname = AppDomain.CurrentDomain.FriendlyName;
if (!File.Exists(file)) file = "app.config".GetFullPath();
if (!File.Exists(file)) file = "{0}.config".F(fname).GetFullPath();
if (!File.Exists(file)) file = "{0}.exe.config".F(fname).GetFullPath();
if (!File.Exists(file)) file = "{0}.dll.config".F(fname).GetFullPath();
Expand All @@ -141,18 +143,44 @@ public static Dictionary<String, String> ConnStrs
var name = item.Attributes["name"]?.Value;
var connstr = item.Attributes["connectionString"]?.Value;
var provider = item.Attributes["providerName"]?.Value;

if (name.IsNullOrEmpty() || connstr.IsNullOrWhiteSpace()) continue;
if (name.EqualIgnoreCase("LocalSqlServer", "LocalMySqlServer")) continue;

var type = DbFactory.GetProviderType(connstr, provider);
if (type == null) XTrace.WriteLine("无法识别{0}的提供者{1}!", name, provider);

cs.Add(name, connstr);
_connTypes.Add(name, type);
cs[name] = connstr;
_connTypes[name] = type;
}
}
}

// 联合使用 appsettings.json
file = "appsettings.json".GetFullPath();
if (File.Exists(file))
{
var dic = new JsonParser(File.ReadAllText(file)).Decode() as IDictionary<String, Object>;
dic = dic?["ConnectionStrings"] as IDictionary<String, Object>;
if (dic != null && dic.Count > 0)
{
foreach (var item in dic)
{
var name = item.Key;
var ds = item.Value as IDictionary<String, Object>;
if (name.IsNullOrEmpty() || ds == null) continue;

var connstr = ds["connectionString"] + "";
var provider = ds["providerName"] + "";
if (connstr.IsNullOrWhiteSpace()) continue;

var type = DbFactory.GetProviderType(connstr, provider);
if (type == null) XTrace.WriteLine("无法识别{0}的提供者{1}!", name, provider);

cs[name] = connstr;
_connTypes[name] = type;
}
}
}

_connStrs = cs;
}
return _connStrs;
Expand Down

0 comments on commit 7bf208d

Please sign in to comment.