我有以下的code用于搜索安装了Microsoft Access驱动程序:
I have the following code that searches for installed Microsoft Access drivers:
var odbcRegKey = Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\ODBC\\ODBCINST.INI\\ODBC Drivers", false);
var drivers = new List<string>(odbcRegKey.GetSubKeyNames());
if (drivers.Contains("Microsoft Access Driver (*.mdb, *.accdb)"))
{
MicrosoftAccessProvider = "Microsoft Access Driver (*.mdb, *.accdb)";
}
else if (drivers.Contains("Microsoft Access Driver (*.mdb)"))
{
MicrosoftAccessProvider = "Microsoft Access Driver (*.mdb)";
}
else
{
//TODO: Throw some kind of excception
}
什么是适当的异常抛出,如果它不能找到ODBC驱动程序?没有公共构造 OdbcException ()
如果有疑问,出现InvalidOperationException是我去到的选择。如果这件事情可配置的(也许能够使用的东西比其他访问),考虑ConfigurationException的为好。
When in doubt, InvalidOperationException is my go-to choice. If it's something configurable (perhaps being able to use something other than Access), consider ConfigurationException as well.