1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using CSFrameworkV5.Core.Common;
using Microsoft.Win32;
///*************************************************************************/
///*
///* 文件名    :SqlConfigWriter.cs                                
///* 程序说明  : SQL连接配置存储策略
///* 原创作者  :孙中吕 
///* 
///* Copyright 2006-2017 C/S框架网 www.csframework.com
///*
///**************************************************************************/
using System;
 
namespace CSFrameworkV5.Core
{
    /// <summary>
    /// 从INI文件读取SQL连接配置
    /// </summary>
    public class IniFileWriter : IWriteSQLConfigValue
    {
        private string _iniFile;
 
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="iniFile">INI文件</param>
        public IniFileWriter(string iniFile)
        {
            _iniFile = iniFile;
            if (!System.IO.File.Exists(iniFile)) Write();
            Read();
        }
 
        public string DatabaseType { get; set; }
 
        public string ConnectionString { get; set; }
 
        public string ServerName { get; set; }
 
        public int Port { get; set; }
 
        public string DatabaseName { get; set; }
 
        public string UserName { get; set; }
 
        public string Password { get; set; }
 
        /// <summary>
        /// 读取配置
        /// </summary>
        public void Read()
        {
            var cfg = new IniFile(_iniFile);
            ConnectionString = KeyProvider.Default.Decrypt(
                cfg.IniReadValue("Connection Settings", "SystemConnection",
                    ""));
            DatabaseType =
                cfg.IniReadValue("Connection Settings", "DatabaseType");
            ServerName = cfg.IniReadValue("Connection Settings", "ServerName");
            Port = ConvertEx.ToInt(cfg.IniReadValue("Connection Settings",
                "Port"));
            DatabaseName =
                cfg.IniReadValue("Connection Settings", "DatabaseName");
            UserName = cfg.IniReadValue("Connection Settings", "UserName");
            Password = KeyProvider.Default.Decrypt(
                cfg.IniReadValue("Connection Settings", "Password", ""));
        }
 
        /// <summary>
        /// 将配置写入INI文件
        /// </summary>
        public void Write()
        {
            var cfg = new IniFile(_iniFile);
            if (cfg != null)
            {
                cfg.IniWriteValue("Connection Settings", "SystemConnection",
                    KeyProvider.Default.Encrypt(ConnectionString));
                cfg.IniWriteValue("Connection Settings", "DatabaseType",
                    DatabaseType.ToStringEx());
                cfg.IniWriteValue("Connection Settings", "ServerName",
                    ServerName.ToStringEx());
                cfg.IniWriteValue("Connection Settings", "Port",
                    Port.ToStringEx());
                cfg.IniWriteValue("Connection Settings", "DatabaseName",
                    DatabaseName.ToStringEx());
                cfg.IniWriteValue("Connection Settings", "UserName",
                    UserName.ToStringEx());
                cfg.IniWriteValue("Connection Settings", "Password",
                    KeyProvider.Default.Encrypt(Password));
            }
        }
 
        /// <summary>
        /// 加载连接配置
        /// </summary>
        /// <param name="ConfigFile">配置文件</param>
        public static IWriteSQLConfigValue LoadConfiguration(string ConfigFile)
        {
            if (System.IO.File.Exists(ConfigFile))
            {
                IWriteSQLConfigValue cfg = new IniFileWriter(ConfigFile);
                return cfg;
            }
            else
            {
                throw new Exception(
                    "Program cann't run without a SQL configuration.You should config the SQL connection by running CSFramework.Tools.SqlConnector.exe!");
            }
        }
    }
 
    /// <summary>
    /// 从系统注册表读取SQL连接配置
    /// </summary>
    public class RegisterWriter : IWriteSQLConfigValue
    {
        private string _keyPath;
 
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="keyPath">注册表位置</param>
        public RegisterWriter(string keyPath)
        {
            _keyPath = keyPath;
            Read();
        }
 
        public string ServerName { get; set; }
 
        public string DatabaseName { get; set; }
 
        public int Port { get; set; }
 
        public string UserName { get; set; }
 
        public string Password { get; set; }
 
        public string DatabaseType { get; set; }
 
        public string ConnectionString { get; set; }
 
        /// <summary>
        /// 读取配置
        /// </summary>
        public void Read()
        {
            var key = Registry.LocalMachine.CreateSubKey(_keyPath);
 
            if (key != null)
            {
                ConnectionString = KeyProvider.Default.Decrypt(
                    ConvertEx.ToString(key.GetValue("ConnectionString", "")));
                DatabaseType =
                    ConvertEx.ToString(key.GetValue("DatabaseType", ""));
                ServerName = ConvertEx.ToString(key.GetValue("ServerName", ""));
                Port = ConvertEx.ToInt(key.GetValue("Port", ""));
                DatabaseName =
                    ConvertEx.ToString(key.GetValue("DatabaseName", ""));
                UserName = ConvertEx.ToString(key.GetValue("UserName", ""));
                Password =
                    KeyProvider.Default.Decrypt(
                        ConvertEx.ToString(key.GetValue("Password", "")));
                key.Close();
            }
        }
 
        /// <summary>
        /// 写入配置
        /// </summary>
        public void Write()
        {
            var key = Registry.LocalMachine.CreateSubKey(_keyPath);
            if (key != null)
            {
                key.SetValue("ConnectionString",
                    KeyProvider.Default.Encrypt(ConnectionString));
                key.SetValue("DatabaseType", DatabaseType);
                key.SetValue("ServerName", ServerName);
                key.SetValue("Port", Port);
                key.SetValue("DatabaseName", DatabaseName);
                key.SetValue("UserName", UserName);
                key.SetValue("Password", KeyProvider.Default.Encrypt(Password));
                key.Close();
            }
        }
    }
 
    /// <summary>
    /// 从web.config文件读取SQL连接配置
    /// </summary>
    public class WebConfigCfg : IWriteSQLConfigValue
    {
        public WebConfigCfg(string databaseType, string connectionString)
        {
            ConnectionString = connectionString;
            DatabaseType = databaseType;
        }
 
        public void Write()
        {
            //
        }
 
        public void Read()
        {
            //
        }
 
        public string ServerName { get; set; }
 
        public string DatabaseName { get; set; }
 
        public int Port { get; set; }
 
        public string UserName { get; set; }
 
        public string Password { get; set; }
 
        public string DatabaseType { get; set; }
 
        public string ConnectionString { get; set; }
    }
}