1. ホーム
  2. データベース
  3. アクセス

レジストリを読み込んで、Officeのバージョンに応じたデータベース接続フィールドを取得します。

2022-01-22 13:52:32
/// <summary> 
//// Read the registry and get the database connection fields based on the Office version 
/// </summary> 
//// <returns>Database connection field</returns> 
private string GetConnectionString() 
{ 
  string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = "; 
 
  RegistryKey rk = Registry.LocalMachine; 
  //query Office2003 
  RegistryKey f03 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\11.0\Access\InstallRoot\"); 
  //Query Office2007 
  RegistryKey f07 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Access\InstallRoot\"); 
  //Query Office2010 
  RegistryKey f10 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\14.0\Access\InstallRoot\"); 
 
  if (f03 ! = null) 
  { 
    strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = "; 
  } 
  else if (f07 ! = null || f10 ! = null) 
  { 
    strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = "; 
  } 
 
  return strConnectionString; 
}