在我們的藍山公司人事管理系統的項目中,員工管理EmployeeManagement和安全管理Security等項目都要用到數據庫連接,考慮到可以將數據庫連接字符串存儲到應用程序配置文件app.config。但默認的應用程序配置文件只能在自己的項目中讀取,如何實現多個項目共享一個
app.config文件,這樣,當數據庫連接發生改變時,只需要修改應用程序配置文件app.config,而不需要重新編譯程序。
一、建立應用程序配置文件app.config
首先,打藍山公司人事管理解決方案,在主項目BlueHillWindows中添加應用程序配置文件app.config,其內容如下:
<?xmlversion="1.0"encoding="utf-8" ?> <configuration> <connectionStrings> <addname="BlueHillConnString"connectionString="Data Source=(local);Initial Catalog=BlueHill;User ID=BlueHill;Password=rj3101"providerName="System.Data.SqlCLient"/> </connectionStrings> </configuration>
該連接字符串命名為BlueHillConnString,內容根據實際情況確定。
編譯時,會在相應的Bin文件夾生成與主程序同名的配置文件,如BlueHillWindows.exe.config,內容與
app.config文件相同。發布時,只要修改BlueHillWindows.exe.config就可以修改數據庫連接字符串。
二、在員工管理EmployeeManagement
項目中讀取主項目中的配置文件
讀取配置文件,需要用到System..Configuration命名空間的
Configuration類。首先,在EmployeeManagement項目中添加對.NET組件System..Configuration的引用。
同樣,在FrmNewEmployee窗體類的構造函數中,也寫入同樣的上述代碼:
public FrmListEmployee() { InitializeComponent(); // 定義Configuration類對象,讀取與主應用程序可執行文件相同文件夾下的配置文件 System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); // 讀取BlueHillConnString數據庫連接字符串,並賦值給SqlConnectiong控件cnBlueHill this.cnBlueHill.ConnectionString = config.ConnectionStrings.ConnectionStrings["BlueHillConnString"].ConnectionString; }
對應修改FrmListEmployee類的構造函數:
public FrmNewEmployee() { InitializeComponent(); // 定義Configuration類對象,讀取與主應用程序可執行文件相同文件夾下的配置文件 System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); // 讀取BlueHillConnString數據庫連接字符串,並賦值給SqlConnectiong控件cnBlueHill this.cnBlueHill.ConnectionString = config.ConnectionStrings.ConnectionStrings["BlueHillConnString"].ConnectionString; // 綁定部門列表 BindDepart(); } public FrmNewEmployee(int empID) { InitializeComponent(); // 定義Configuration類對象,讀取與主應用程序可執行文件相同文件夾下的配置文件 System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); // 讀取BlueHillConnString數據庫連接字符串,並賦值給SqlConnectiong控件cnBlueHill this.cnBlueHill.ConnectionString = config.ConnectionStrings.ConnectionStrings["BlueHillConnString"].ConnectionString; // 存儲要操作的員工號,並記錄操作方式為更新 this.EmployeeID = empID; this.Operator = OperatorType.Update; // 綁定員工列表和員工信息 BindDepart(); BindEmployee(this.EmployeeID); this.Text = "修改員工信息"; tbPassword.CausesValidation = false; tbPassword.Enabled = false; }
三、在安全管理Security
項目中讀取主項目中的配置文件
與第二步類似,在Security項目中添加對.NET組件System..Configuration的引用。
同樣,在FrmLogin窗體類的構造函數中,也寫入同樣的上述代碼:
public FrmLogin() { InitializeComponent(); // 定義Configuration類對象,讀取與主應用程序可執行文件相同文件夾下的配置文件 System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); // 讀取BlueHillConnString數據庫連接字符串,並賦值給SqlConnectiong控件cnBlueHill this.cnBlueHill.ConnectionString = config.ConnectionStrings.ConnectionStrings["BlueHillConnString"].ConnectionString; }
以后,如果在調試期間調整數據庫連接字符串,修改主項目的
app.config文件中對應的數據庫連接字符串就要以了,應用程序無需任何修改。例如,要使用Windows身份認證,將連接字符串:
<
add
name
=
"BlueHillConnString"connectionString="Data Source=(local);Initial Catalog=BlueHill;User ID=BlueHill;Password=rj3101"providerName="System.Data.SqlCLient"/>
修改為:
<
add
name
=
"BlueHillConnString"connectionString="Data Source=(local);Initial Catalog=BlueHill;
Integrated Security=SSPI;
"providerName="System.Data.SqlCLient"/>
如果要使用SQL Server Express數據庫,數據庫example.mdf與可執行文件在同一文件夾,則可以將連接字符串改為:
<
add
name
=
"BlueHillConnString"connectionString="
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\example.mdf;Integrated Security=True;User Instance=True
"providerName="System.Data.SqlCLient"/>
如果項目已經發布,也只需要修改可執行文件夾下與可執行文件同名的.config文件中對應的內容就可以了。