forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseDBConfig.cs
More file actions
79 lines (67 loc) · 2.88 KB
/
BaseDBConfig.cs
File metadata and controls
79 lines (67 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.IO;
namespace Blog.Core.Common.DB
{
public class BaseDBConfig
{
private static string sqliteConnection = Appsettings.app(new string[] { "AppSettings", "Sqlite", "SqliteConnection" });
private static bool isSqliteEnabled = (Appsettings.app(new string[] { "AppSettings", "Sqlite", "Enabled" })).ObjToBool();
private static string sqlServerConnection = Appsettings.app(new string[] { "AppSettings", "SqlServer", "SqlServerConnection" });
private static bool isSqlServerEnabled = (Appsettings.app(new string[] { "AppSettings", "SqlServer", "Enabled" })).ObjToBool();
private static string mySqlConnection = Appsettings.app(new string[] { "AppSettings", "MySql", "MySqlConnection" });
private static bool isMySqlEnabled = (Appsettings.app(new string[] { "AppSettings", "MySql", "Enabled" })).ObjToBool();
private static string oracleConnection = Appsettings.app(new string[] { "AppSettings", "Oracle", "OracleConnection" });
private static bool IsOracleEnabled = (Appsettings.app(new string[] { "AppSettings", "Oracle", "Enabled" })).ObjToBool();
public static string ConnectionString => InitConn();
public static DataBaseType DbType = DataBaseType.SqlServer;
private static string InitConn()
{
if (isSqliteEnabled)
{
DbType = DataBaseType.Sqlite;
return sqliteConnection;
}
else if (isSqlServerEnabled)
{
DbType = DataBaseType.SqlServer;
return DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1.txt", @"c:\my-file\dbCountPsw1.txt", sqlServerConnection);
}
else if (isMySqlEnabled)
{
DbType = DataBaseType.MySql;
return DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_MySqlConn.txt", @"c:\my-file\dbCountPsw1_MySqlConn.txt", mySqlConnection);
}
else if (IsOracleEnabled)
{
DbType = DataBaseType.Oracle;
return DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_OracleConn.txt", @"c:\my-file\dbCountPsw1_OracleConn.txt", oracleConnection);
}
else
{
return "server=.;uid=sa;pwd=sa;database=WMBlogDB";
}
}
private static string DifDBConnOfSecurity(params string[] conn)
{
foreach (var item in conn)
{
try
{
if (File.Exists(item))
{
return File.ReadAllText(item).Trim();
}
}
catch (System.Exception) { }
}
return conn[conn.Length - 1];
}
}
public enum DataBaseType
{
MySql = 0,
SqlServer = 1,
Sqlite = 2,
Oracle = 3,
PostgreSQL = 4
}
}