.NET中ODBC驱动列表有没有办法从.NET获取安装在WindowsXP计算机上的ODBC驱动列表?基本上我想看到的(在.NET中):控制面板->管理工具->数据源(ODBC)->“驱动程序”选项卡。看这个或这个基本上,系统将ODBC驱动程序的信息存储在HKEY_LOCAL_MACHINESOFTWAREODBCODBCINST.INIODBC驱动程序中您可以使用此代码或类似代码来查找已安装的ODBC驱动程序。这段代码基本上是从注册表中读取驱动程序信息publicstaticListGetSystemDriverList(){Listnames=newList();//获取系统dsn的Microsoft.Win32.RegistryKeyreg=(Microsoft.Win32.Registry.LocalMachine)。OpenSubKey("软件");如果(reg!=null){reg=reg.OpenSubKey("ODBC");if(reg!=null){reg=reg.OpenSubKey("ODBCINST.INI");if(reg!=null){reg=reg.OpenSubKey("ODBC驱动程序");if(reg!=null){//获取DSN_LOC_IN_REGISTRY中定义的所有DSN条目。foreach(stringsNameinreg.GetValueNames()){names.Add(sName);}}尝试{reg.Close();}catch{/*如果我们不能关闭就忽略这个异常*/}}}}returnnames;没有必要打开每个中间子项。读取注册表项以获取ODBC驱动程序名称可以以更紧凑的方式完成,如下所示://////从注册表中获取ODBC驱动程序名称。//////如果注册表项存在,则包含ODBC驱动程序名称的字符串数组;空,否则。publicstaticstring[]GetOdbcDriverNames(){string[]odbcDriverNames=null;使用(RegistryKeylocalMachineHive=Registry.LocalMachine)使用(RegistryKeyodbcDriversKey=localMachineHive.OpenSubKey(@"SOFTWAREODBCODBCINST.INIODBCDrivers")){if(odbcDriversKey!=null){odbcDriverNames=odbcDriversKey.GetValueNames();}}返回odbcDriverNames;您还可以通过对SQLGetInstalledDriversW执行P/Invoke来实现此目的:ushortpcbBufOut);//////从SQLGetInstalledDrivers函数获取ODBC驱动程序名称。//////包含ODBC驱动程序名称的字符串数组,如果调用SQLGetInstalledDrivers成功;空,否则。publicstaticstring[]GetOdbcDriverNames(){string[]odbcDriverNames=null;char[]driverNamesBuffer=newchar[ushort.MaxValue];短款;boolsucceeded=SQLGetInstalledDriversW(driverNamesBuffer,outsize);if(succeeded==true){char[]driverNames=newchar[size-1];Array.Copy(driverNamesBuffer,driverNames,size-1);odbcDriverNames=(newstring(driverNames)).Split('');}返回odbcDriverNames;我也是在创建ODBC数据源时调用了这个函数,并使用结果优雅地降级到早期版本的SQL驱动:如果对你有用,需要了解更多C#学习教程,希望你付出moreattentiontoit—//////获取MicrosoftSQLServer的ODBC驱动程序的名称,优先考虑最新的驱动程序。//////MicrosoftSQLServer的ODBC驱动程序的名称,如果存在的话;空,否则。publicstaticstringGetOdbcSqlDriverName(){ListdriverPrecedence=newList(){"SQLServerNativeClient11.0","SQLServerNativeClient10.0","SQLServerNativeClient9.0","SQLServer"};string[]availableOdbcDrivers=GetOdbcDriverNames();stringdriverName=null;if(availableOdbcDrivers!=null){driverName=driverPrecedence.Intersect(availableOdbcDrivers).FirstOrDefault();}returndriverName;}本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除,如需转载请注明出处:
