private List <string> StartNetworkScan(CancellationToken ct) { List <string> localNetworks = new List <string>(); if (ct.IsCancellationRequested) { return(localNetworks); } using (OleDbDataReader reader = OleDbEnumerator.GetEnumerator(Type.GetTypeFromProgID("SQLOLEDB Enumerator"))) { while (reader.Read()) { if (ct.IsCancellationRequested) { return(localNetworks); } object[] row = new object[reader.FieldCount]; reader.GetValues(row); string networkHost = (string)row[0]; if (!localNetworks.Contains(networkHost)) { localNetworks.Add(networkHost); } } } return(localNetworks); }
private void FillDataSources() { DataTable table = new DataTable(); table.Locale = CultureInfo.InvariantCulture; try { OleDbDataReader enumerator = OleDbEnumerator.GetEnumerator( Type.GetTypeFromCLSID(new Guid("C8B522CD-5CF3-11ce-ADE5-00AA0044773D"))); using (enumerator) { table.Load(enumerator); } } catch { } cbxDsName.Items.Clear(); foreach (DataRow row in table.Rows) { cbxDsName.Items.Add(row["SOURCES_NAME"].ToString()); } cbxDsName.Sorted = true; }
/// <summary> /// Gets the registered OLE DB providers as an array of ProgIDs. /// </summary> public static List <string> GetRegisteredProviders() { // Get the sources rowset for the root OLE DB enumerator Type rootEnumeratorType = Type.GetTypeFromCLSID(NativeMethods.CLSID_OLEDB_ENUMERATOR); OleDbDataReader dr = OleDbEnumerator.GetEnumerator(rootEnumeratorType); // Read the CLSIDs of each data source (not binders or enumerators) Dictionary <string, string> sources = new Dictionary <string, string>(); // avoids duplicate entries using (dr) { while (dr.Read()) { int type = (int)dr["SOURCES_TYPE"]; if (type == NativeMethods.DBSOURCETYPE_DATASOURCE_TDP || type == NativeMethods.DBSOURCETYPE_DATASOURCE_MDP) { sources[dr["SOURCES_CLSID"] as string] = null; } } } // reader is disposed here // Get the ProgID for each data source List <string> sourceProgIds = new List <string>(sources.Count); Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID"); using (key) { foreach (KeyValuePair <string, string> source in sources) { Microsoft.Win32.RegistryKey subKey = key.OpenSubKey(source.Key + "\\ProgID"); // if this subkey does not exist, ignore it. if (subKey != null) { using (subKey) { sourceProgIds.Add(subKey.GetValue(null) as string); } // subKey is disposed here } } } // key is disposed here // Sort the prog ID array by name sourceProgIds.Sort(); // The OLE DB provider for ODBC is not supported by the OLE DB .NET provider, so remove it while (sourceProgIds.Contains("MSDASQL.1")) { sourceProgIds.Remove("MSDASQL.1"); } return(sourceProgIds); }
public void GetEnumerator_BadType_Throws() { Assert.Throws <ArgumentException>(() => OleDbEnumerator.GetEnumerator(typeof(Exception))); }
private void EnumerateProviders() { OleDbDataReader dr = null; try { // Get the sources rowset for the root OLE DB enumerator dr = OleDbEnumerator.GetEnumerator(Type.GetTypeFromCLSID(NativeMethods.CLSID_OLEDB_ENUMERATOR)); // Get the CLSIDs and descriptions of each data source (not binders or enumerators) Dictionary <string, string> sources = new Dictionary <string, string>(); // avoids duplicate entries while (dr.Read()) { int type = dr.GetInt32(dr.GetOrdinal("SOURCES_TYPE")); if (type == NativeMethods.DBSOURCETYPE_DATASOURCE_TDP || type == NativeMethods.DBSOURCETYPE_DATASOURCE_MDP) { string clsId = dr["SOURCES_CLSID"].ToString(); string description = dr["SOURCES_DESCRIPTION"].ToString(); sources[clsId] = description; } } // Get the full ProgID for each data source Dictionary <string, string> sourceProgIds = new Dictionary <string, string>(sources.Count); _providers = new List <ProviderType>(); Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID"); using (key) { foreach (KeyValuePair <string, string> source in sources) { Microsoft.Win32.RegistryKey subKey = key.OpenSubKey(source.Key + "\\ProgID"); if (subKey != null) { using (subKey) { string progId = key.OpenSubKey(source.Key + "\\ProgID").GetValue(null) as string; if (progId != null && !progId.Equals("MSDASQL", StringComparison.OrdinalIgnoreCase) && !progId.StartsWith("MSDASQL.", StringComparison.OrdinalIgnoreCase) && !progId.Equals("Microsoft OLE DB Provider for ODBC Drivers")) { sourceProgIds[progId] = source.Key; } } // subKey is disposed here } } } // key is disposed here // Populate the combo box foreach (KeyValuePair <string, string> entry in sourceProgIds) { _providers.Add(new ProviderType() { Name = entry.Key, DisplayName = sources[entry.Value] }); } _providers = _providers.OrderBy(x => x.DisplayName).ToList <ProviderType>(); } finally { if (dr != null) { dr.Dispose(); } } }