App.config中的数据库连接字符串,是在Settings.settings同步过来的。后者在设计时支持数据集DataSet的设计,但运行时不能更改,可以更改App.config的连接字符串,但无法加密、解密。造成的结果是数据库连接字符串,要么固定不变,要么以明文显示,极不安全。多次尝试后,成功的解决这些问题。 我的经验:
6 _: }8 r e; ~6 ^" H 1. Settings.settings中的设置和App.config中的设置会相互更新,彼此同步。前者用于设计支持,后者用于运行时。可以手工阻止两者同步,使两者设置不一样,如果没有发布App.config,则全部采用Settings.settings的设置;否则在运行时App.config会覆盖Settings.settings的设置。2 _9 Z/ ], C$ J
2. Settings.settings中的Application范围设置只在设计时更改,是只读的属性,User范围设置可以在运行时读写。* ?7 r) ~0 } b, h' V/ b5 m
应用程序作用域设置与用户作用域设置之间的重要区别是,用户作用域设置在运行时为读/写,并且可在代码中对其值进行更改和保存。应用程序作用域设置在运行时为只读。虽然可以读取,但是不能对其进行写入。具有应用程序作用域的设置只能在设计时或通过手动修改设置文件进行更改。9 ?. D3 w ~5 b$ t
3. Settings中的只有“连接字符串”设置才能被数据集DataSet设计时支持,而“连接字符串”设置只能是Application范围设置,是只读的属性。
8 _0 A# O8 b) d% a% ~# [: _ 解决思路:
5 f; i$ g) L4 o& t$ v3 | 将Settings.settings的设计时代码文件Settings.Designer.cs内容复制到Setgings.cs中,删除Settings.Designer.cs,更改部分代码。连接字符串在Setgings.cs中缺省设置为明码,在App.config中为加密码。代码如下,DESEncrypt为静态加密函数,DESDecrypt为静态解密函数:+ z. \$ T3 A+ | s" {
重写Settings代码1 L$ M# X' ?4 L, f5 @
1 a2 d5 W1 g& g# C* `# f# _& k6 d: v0 o$ E' o+ H' T
1 using System.Xml; 2 namespace A.Properties 3{ 4 5 [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 6 [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.8 F0 q: d) K3 y: I7 B7 [- v" A
VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator",
5 j. q( o- S5 j2 E( c5 | "8.0.0.0")] 7 internal sealed class Settings : global::System.Configuration.
3 T$ z, u: b7 R( I, a& R ApplicationSettingsBase 8 { 910 private static Settings defaultInstance = ((Settings)(global::System.) }! c6 N! Y6 P6 r3 Z
Configuration.ApplicationSettingsBase.Synchronized(new Settings())));1112 public static Settings Default13 {14 get15 {16 return defaultInstance;17 }18 }1920 [global::System.Configuration.ApplicationScopedSettingAttribute()]21 [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]22 [global::System.Configuration.SpecialSettingAttribute(global::System.
( ]; f5 v) a+ \& w. q Configuration.SpecialSetting.ConnectionString)]23 [global::System.Configuration.DefaultSettingValueAttribute* j: u% D ` q
("Data Source=(local);Initial Catalog=Test;Persist Security Info=% `' c- P2 N1 m' C8 x
True;User ID=sa;Password=")]24 public string ConnectionString125 {26 get27 {28 return Encrypt.DESDecrypt(this["ConnectionString1"].ToString());29 }30 set31 {32 base["ConnectionString1"] = Encrypt.DESEncrypt(value);33 this.SetKeyValue("A.Properties.Settings.ConnectionString1", 8 P; x# l1 I' y% n, \% j
Encrypt.DESEncrypt(value));34 }35 }363738 /**//// 39 /// 保存设置40 /// 41 ///
' E5 [; @. A2 B6 l, E- G7 f6 ^42 ///
) ^0 }. T4 G! p2 d43 private void SetKeyValue(string AppKey, string AppValue)44 {45 XmlDocument xDoc = new XmlDocument();46 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");4748 XmlNode xNode;49 XmlElement xElem1;50 XmlElement xElem2;5152 xNode = xDoc.SelectSingleNode("//connectionStrings");5354 xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@name='" +5 u! k$ c4 M B9 T A0 u; o
AppKey + "']");55 if (xElem1 != null) xElem1.SetAttribute("connectionString", AppValue);56 else57 {58 xElem2 = xDoc.CreateElement("add");59 xElem2.SetAttribute("name", AppKey);60 xElem2.SetAttribute("connectionString", AppValue);61 xNode.AppendChild(xElem2);62 }63 xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");64 }656667 }68}69
9 `6 b7 W% X* s, t; h 默认情况下,新的连接字符串设置只在重启时生效。但可以操作数据前,加上一条更改Dataset中Adapter的连接字符串的语句,就可以不用重启,代码如下:
& r+ w3 d, M: }, s2 J 运行更改数据库连接3 h- V. p6 m. T# ~' r+ j( u8 O/ H
$ n$ N/ d5 x/ g4 C9 W
' v/ g, A0 l# z, E' v 1 private void toolStripButton1_Click(object sender, EventArgs e) 2 { 3 A.Properties.Settings.Default.ConnectionString1 = @"Data Source=; g9 n/ \' I: j/ h
(local);Initial Catalog=Test2;Persist Security Info=True;User ID=sa"; 4 5 this.toolStripTextBox1.Text = A.Properties.Settings.Default.# B' L' ~9 a- _6 x
ConnectionString1; 6 this.Test1TableAdapter.Connection.ConnectionString =
9 D+ T1 s$ ^# R4 \4 M A.Properties.Settings.Default.ConnectionString1; 7 MessageBox.Show(A.Properties.Settings.Default.ConnectionString1); 8 this.Test1TableAdapter.Fill(this.dataSet1.Test1); 910 } |