App.config中的数据库连接字符串,是在Settings.settings同步过来的。后者在设计时支持数据集DataSet的设计,但运行时不能更改,可以更改App.config的连接字符串,但无法加密、解密。造成的结果是数据库连接字符串,要么固定不变,要么以明文显示,极不安全。多次尝试后,成功的解决这些问题。
我的经验:
1. Settings.settings中的设置和App.config中的设置会相互更新,彼此同步。前者用于设计支持,后者用于运行时。可以手工阻止两者同步,使两者设置不一样,如果没有发布App.config,则全部采用Settings.settings的设置;否则在运行时App.config会覆盖Settings.settings的设置。
2. Settings.settings中的Application范围设置只在设计时更改,是只读的属性,User范围设置可以在运行时读写。参考MSDN文章“在 C# 中使用设置”中的说明:
应用程序作用域设置与用户作用域设置之间的重要区别是,用户作用域设置在运行时为读/写,并且可在代码中对其值进行更改和保存。应用程序作用域设置在运行时为只读。虽然可以读取,但是不能对其进行写入。具有应用程序作用域的设置只能在设计时或通过手动修改设置文件进行更改。
3. Settings中的只有“连接字符串”设置才能被数据集DataSet设计时支持,而“连接字符串”设置只能是Application范围设置,是只读的属性。
解决思路:
将Settings.settings的设计时代码文件Settings.Designer.cs内容复制到Setgings.cs中,删除Settings.Designer.cs,更改部分代码。连接字符串在Setgings.cs中缺省设置为明码,在App.config中为加密码。代码如下,DESEncrypt为静态加密函数,DESDecrypt为静态解密函数:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
4
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
14
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
16
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
26
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
28
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
32
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
39
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
45
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
58
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
69
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
默认情况下,新的连接字符串设置只在重启时生效。但可以操作数据前,加上一条更改Dataset中Adapter的连接字符串的语句,就可以不用重启,代码如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
3
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)