Moli0
2021-12-15 24d68e7998e80cbd4ef3ed04acbf2539a9c5a46b
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace PublicCode
{
    /// <summary>
    /// 数据库连接配置类
    /// </summary>
    public class DBConfig
    {
 
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private string ConnectionStr { get; set; }
        /// <summary>
        /// 数据库服务器主机
        /// </summary>
        private string DBHost { get; set; }
        /// <summary>
        /// 数据库登录名
        /// </summary>
        private string ConnectionUserid { get; set; }
        /// <summary>
        /// 数据库登录密码
        /// </summary>
        private string ConnectionPwd { get; set; }
        /// <summary>
        /// 多数据库数据名称字典
        /// </summary>
        private Dictionary<string,string> ConnectionDBName { get; set; }
        /// <summary>
        /// 当前连接的数据库
        /// </summary>
        private string NowDBName { get; set; }
        /// <summary>
        /// 系统主数据库
        /// </summary>
        private string MainDBName { get; set; }
 
        /// <summary>
        /// 构造函数,构造时会初始化连接参数,而后可以调用InitConn返回打开的连接对象
        /// </summary>
        public DBConfig() {
            //以下配置建议写在配置文件中,再读取相应的配置文件
            DBHost = "linmoli.cn,53101";
            ConnectionUserid = "sa";
            ConnectionPwd = "123456..";
            MainDBName = "DreamHeaven";
            if (ConnectionDBName == null) {
                ConnectionDBName = new Dictionary<string, string>();
            }
        }
 
        /// <summary>
        /// 初始化数据库连接字符串
        /// </summary>
        /// <param name="type">配置中的数据库</param>
        /// <returns></returns>
        private void Init(string type)
        {
            if (string.IsNullOrWhiteSpace(type))
            {
                NowDBName = MainDBName;
            }
            else {
                ConnectionDBName.Remove(type);
                ConnectionDBName.Add(type, type);
                if (string.IsNullOrWhiteSpace(ConnectionDBName[type])) {
                    throw new Exception("未配置的数据库名称\r\nDatabase Name Is Invalid!");
                }
                NowDBName = ConnectionDBName[type];
            }
            ConnectionStr = $"server={DBHost};uid={ConnectionUserid};pwd={ConnectionPwd};database={NowDBName}";
        }
 
        /// <summary>
        /// 初始化数据库连接
        /// </summary>
        /// <param name="DBName">配置中的数据库</param>
        /// <returns>返回连接的数据库对象</returns>
        public SqlConnection InitConn(string DBName="")
        {
            Init(DBName);
            SqlConnection conn = new SqlConnection(ConnectionStr);
            conn.Open();
            return conn;
        }
    }
}