public static OdbcDsn ParseForODBCDSN(string dsnName, string dsnDriverName, Dictionary <string, string> data) { OdbcDsn odbcdsn = new OdbcDsn() { DSNName = dsnName, DSNDriverName = dsnDriverName, Data = data }; // For each element defined for a typical DSN get // its value. foreach (var dsnElement in data) { string v = dsnElement.Value; switch (dsnElement.Key.ToLower()) { case "description": odbcdsn.Description = v; break; case "server": case "servername": odbcdsn.ServerName = v; break; case "driver": odbcdsn.DSNDriverPath = v; break; case "uid": odbcdsn.UID = v; break; case "pwd": odbcdsn.Password = v; break; case "databasename": odbcdsn.DatabaseName = v; break; case "commlinks": odbcdsn.CommLinks = v; break; case "host": odbcdsn.Host = v; break; } } return(odbcdsn); }
/// <summary> /// Method that gives one ODBC DSN object /// </summary> /// <param name="baseKey"></param> /// <param name="dsnName"></param> /// <returns>ODBC DSN object</returns> private static OdbcDsn GetDSN(RegistryKey baseKey, string dsnName) { string dsnDriverName; // Get the key for (using the baseKey parmetre passed in) // "\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\\" (DSN_LOC_IN_REGISTRY) // that contains all the configured Data Source Name (DSN) entries. using (RegistryKey dsnNamesKey = baseKey.OpenSubKey(DSN_LOC_IN_REGISTRY, false)) { if (dsnNamesKey == null) { return(null); } object value = dsnNamesKey.GetValue(dsnName); // Get the name of the driver for which the DSN is // defined. dsnDriverName = (value != null) ? value.ToString() : null; } // Get the key for ODBC_INI_LOC_IN_REGISTRY+dsnName. using (RegistryKey dsnNameKey = baseKey.OpenSubKey(ODBC_INI_LOC_IN_REGISTRY + dsnName, false)) { OdbcDsn odbcDSN = null; if (dsnNameKey != null) { // Get all elements defined in the above key string[] dsnElements = dsnNameKey.GetValueNames(); Dictionary <string, string> data = new Dictionary <string, string>(dsnElements.Length); // For each element defined for a typical DSN get // its value. foreach (string dsnElement in dsnElements) { data.Add(dsnElement, dsnNameKey.GetValue(dsnElement).ToString()); } // Create ODBCDSN Object. odbcDSN = OdbcDsn.ParseForODBCDSN(dsnName, dsnDriverName, data); } return(odbcDSN); } }
/// <summary> /// Method that gives the Data Source Name (DSN) entries as list of /// ODBCDSN objects. /// </summary> /// <returns>list of DSNs based on the baseKey parameter</returns> private static List <OdbcDsn> GetDSNList(RegistryKey baseKey) { if (baseKey == null) { return(null); } List <OdbcDsn> dsnList = new List <OdbcDsn>(); // Get the key for (using the baseKey parmetre passed in) // "\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\\" (DSN_LOC_IN_REGISTRY) // that contains all the configured Data Source Name (DSN) entries. using (RegistryKey dsnNamesKey = baseKey.OpenSubKey(DSN_LOC_IN_REGISTRY, false)) { if (dsnNamesKey == null) { return(null); } // Get all DSN entries defined in DSN_LOC_IN_REGISTRY. string[] dsnNames = dsnNamesKey.GetValueNames(); if (dsnNames != null) { // Foreach DSN entry in the DSN_LOC_IN_REGISTRY, goto the // Key ODBC_INI_LOC_IN_REGISTRY+dsnName and get elements of // the DSN entry to create ODBCDSN objects. foreach (string dsnName in dsnNames) { // Get ODBC DSN object. OdbcDsn odbcDSN = GetDSN(baseKey, dsnName); if (odbcDSN != null) { dsnList.Add(odbcDSN); } } } return(dsnList); } }
public static OdbcDsn ParseForODBCDSN(string dsnName, string dsnDriverName, Dictionary<string, string> data) { OdbcDsn odbcdsn = new OdbcDsn() { DSNName = dsnName, DSNDriverName = dsnDriverName, Data = data }; // For each element defined for a typical DSN get // its value. foreach (var dsnElement in data) { string v = dsnElement.Value; switch (dsnElement.Key.ToLower()) { case "description": odbcdsn.Description = v; break; case "server": case "servername": odbcdsn.ServerName = v; break; case "driver": odbcdsn.DSNDriverPath = v; break; case "uid": odbcdsn.UID = v; break; case "pwd": odbcdsn.Password = v; break; case "databasename": odbcdsn.DatabaseName = v; break; case "commlinks": odbcdsn.CommLinks = v; break; case "host": odbcdsn.Host = v; break; } } return odbcdsn; }