01 /// <summary>
02 /// 讀取注冊表,根據(jù)Office版本獲取數(shù)據(jù)庫連接字段
03 /// </summary>
04 /// <returns>數(shù)據(jù)庫連接字段</returns>
05 private string GetConnectionString()
06 {
07 string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ";
08
09 RegistryKey rk = Registry.LocalMachine;
10 //查詢Office2003
11 RegistryKey f03 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\11.0\Access\InstallRoot\");
12 //查詢Office2007
13 RegistryKey f07 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Access\InstallRoot\");
14 //查詢Office2010
15 RegistryKey f10 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\14.0\Access\InstallRoot\");
16
17 if (f03 != null)
18 {
19 strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ";
20 }
21 else if (f07 != null || f10 != null)
22 {
23 strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = ";
24 }
25
26 return strConnectionString;
27 }
更多信息請查看IT技術(shù)專欄