如何在.NET中读取ODBC数据源名?

事实上,所有的ODBC数据源名都存放在Windows系统的注册表中。如果你知道注册表中正确的项值,你就可以通过.NET  Framework类库提供的注册表类来读取该项值下的所有DSN列表。

所有ODBC数据源名都存放在Windows注册表下的:LocalMachine\ODBC\ODBC.INI\ODBC  Data  Sources(系统DSN)和CurrentUser\Software\ODBC\ODBC.INI\ODBC  Data  Sources(用户DSN)  键值中。

Imports  Microsoft.Win32  '引用名字空间      
 
  下面的源代码是演示读取ODBC  DSN列表内容并加入到ListBox控件中。来测试这段代码,建立一个Windows  应用程式,添加一个ListBox控件到窗体表单中,并将ReadODBCDSNs方法加到程式代码中。然后,可用一个命令按钮的单或双击事件或用窗体的导入事件来引用这个方法。

[C#]
 1 None.gif private   void  ReadODBCSNs() 
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif
 3InBlock.gif            string str; 
 4InBlock.gif            RegistryKey rootKey; 
 5InBlock.gif            RegistryKey subKey; 
 6InBlock.gif            string[] dsnList; 
 7InBlock.gif            rootKey = Registry.LocalMachine; 
 8InBlock.gif            str = "SOFTWARE\\\\ODBC\\\\ODBC.INI\\\\ODBC Data Sources"
 9InBlock.gif            subKey = rootKey.OpenSubKey(str); 
10InBlock.gif            dsnList = subKey.GetValueNames(); 
11InBlock.gif            ListBox1.Items.Add("System DSNs"); 
12InBlock.gif            ListBox1.Items.Add("================"); 
13InBlock.gif            //string dsnName; 
14InBlock.gif            foreach (string strdsnName in dsnList) 
15ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
16InBlock.gif                ListBox1.Items.Add(strdsnName); 
17ExpandedSubBlockEnd.gif            }
 
18InBlock.gif            subKey.Close(); 
19InBlock.gif            rootKey.Close(); 
20InBlock.gif            rootKey = Registry.CurrentUser; 
21InBlock.gif            str = "SOFTWARE\\\\ODBC\\\\ODBC.INI\\\\ODBC Data Sources"
22InBlock.gif            subKey = rootKey.OpenSubKey(str); 
23InBlock.gif            dsnList = subKey.GetValueNames(); 
24InBlock.gif            ListBox1.Items.Add("================"); 
25InBlock.gif            ListBox1.Items.Add("UserDSNs"); 
26InBlock.gif            ListBox1.Items.Add("================"); 
27InBlock.gif            foreach (string dsnName in dsnList) 
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
29InBlock.gif                ListBox1.Items.Add(dsnName); 
30ExpandedSubBlockEnd.gif            }
 
31InBlock.gif            subKey.Close(); 
32InBlock.gif            rootKey.Close(); 
33ExpandedBlockEnd.gif        }

[VB.NET]
 1 ExpandedBlockStart.gif ContractedBlock.gif Private    Sub ReadODBCSNs() Sub  ReadODBCSNs()    
 2InBlock.gif 
 3InBlock.gifDim  str  As  String    
 4InBlock.gifDim  rootKey  As  RegistryKey,  subKey  As  RegistryKey    
 5InBlock.gifDim  dsnList()  As  String    
 6InBlock.gifrootKey  =  Registry.LocalMachine    
 7InBlock.gifstr  =  "SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources"    
 8InBlock.gifsubKey  =  rootKey.OpenSubKey(str)    
 9InBlock.gifdsnList  =  subKey.GetValueNames()    
10InBlock.gifListBox1.Items.Add("System DSNs")    
11InBlock.gifListBox1.Items.Add("================")    
12InBlock.gifDim  dsnName  As  String    
13InBlock.gif 
14InBlock.gifFor  Each  dsnName  In  dsnList    
15InBlock.gifListBox1.Items.Add(dsnName)    
16InBlock.gifNext    
17InBlock.gifsubKey.Close()    
18InBlock.gif 
19InBlock.gifrootKey.Close()    
20InBlock.gif 
21InBlock.gif'Load  User  DSNs    
22InBlock.gifrootKey  =  Registry.CurrentUser    
23InBlock.gifstr  =  "SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources"    
24InBlock.gifsubKey  =  rootKey.OpenSubKey(str)    
25InBlock.gifdsnList  =  subKey.GetValueNames()    
26InBlock.gif 
27InBlock.gifListBox1.Items.Add("================")    
28InBlock.gifListBox1.Items.Add("UserDSNs")    
29InBlock.gifListBox1.Items.Add("================")    
30InBlock.gif 
31InBlock.gifFor  Each  dsnName  In  dsnList    
32InBlock.gifListBox1.Items.Add(dsnName)    
33InBlock.gifNext    
34InBlock.gifsubKey.Close()    
35InBlock.gif 
36InBlock.gifrootKey.Close()    
37ExpandedSubBlockStart.gifContractedSubBlock.gifEnd  Sub ()Sub    

运行效果图
o_odbc.bmp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值