问题描述
我有一个使用OleDb 12.0驱动程序连接到MS Access数据库的C#应用程序.如果未安装OleDb 12.0驱动程序,则该应用程序将引发一个异常,该异常没有解释性的解释.
I have a C# application which uses OleDb 12.0 driver to connect to an MS Access database. If the OleDb 12.0 driver is not installed, the application throws an exception which is not relevantly explanatory.
public static class Program
{
private static Mutex mutex = null;
[STAThread]
static void Main()
{
try
{
InMemoryValues.CorrectnessRepetition = 15;
InMemoryValues.MultipleChoiceCount = 6;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
... ...
... ...
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CollectionForm());
}
}
我想让用户知道没有驱动程序的特定问题.我不想对信息进行硬编码.
I want to let users know about the specific problem of not having a driver. I don't want to hard-code the information.
我应该怎么做?
相关源代码
public static class InMemoryValues
{
private static ApplicationData _appData = null;
public static int CorrectnessRepetition { get; set; }
public static int MultipleChoiceCount { get; set; }
static InMemoryValues()
{
_appData = ApplicationDataBLLL.Get();
if (_appData == null)
{
_appData = new ApplicationData();
}
}
public static ApplicationData ApplicationData
{
set
{
_appData = value;
}
get
{
return _appData;
}
}
public static void Save()
{
ApplicationDataBLLL.Save(_appData);
}
}
推荐答案
使用 OleDbEnumerator.GetElements()
您可以获得 DataTable
,其中包含所有可见的OLE DB提供程序的列表.返回的数据表的第一列是 SOURCES_NAME
,它是本机OLEDB数据源或枚举器的不变名称.
Using OleDbEnumerator.GetElements()
you can get a DataTable
which contains list of all visible OLE DB providers.The first column of the returned data table, is SOURCES_NAME
which is the invariant name of the native OLEDB data source or enumerator.
因此,您可以使用以下代码来查找是否已安装 Microsoft.ACE.OLEDB.12.0
并对执行过程可见:
So you can use the following code to find out if Microsoft.ACE.OLEDB.12.0
is installed and visible to the executing process:
var oledb12Installed = new System.Data.OleDb.OleDbEnumerator()
.GetElements().AsEnumerable()
.Any(x => x.Field<string>("SOURCES_NAME") ==
"Microsoft.ACE.OLEDB.12.0");
位数
请记住,如果在安装 Microsoft.ACE.OLEDB.12.0
的X64版本的同时为X86编译应用程序,则上述代码将返回false,这意味着您的应用程序无法使用 Microsoft.ACE.OLEDB.12.0
.
Keep in mind, if you are compiling the application for X86 while you have installed X64 versions of the Microsoft.ACE.OLEDB.12.0
, then above code will return false which means your application cannot use Microsoft.ACE.OLEDB.12.0
.
您需要使用与您的应用程序相同的位的软件包.您可以从以下链接下载提供程序:
You need to use the package with the same bitness as your application. You can download the provider from the following link:
点击下载"按钮后,您可以选择下载x86或x64.
After clicking on Download button, you have the choice to download x86 or x64.
这篇关于检查是否安装了用于MS Access的OleDb 12.0驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!