/// <summary>
        /// Converts ODBC connection string to the SQL Client (.NET).
        /// </summary>
        public static string OdbcToSqlClient(this string odbcString)
        {
            if (string.IsNullOrEmpty(odbcString))
            {
                return(null);
            }
            try {
                var odbc     = new OdbcConnectionStringBuilder(odbcString);
                var server   = odbc.GetValue(OdbcServerKey);
                var database = odbc.GetValue(OdbcDatabaseKey);
                if (!string.IsNullOrWhiteSpace(server) && !string.IsNullOrWhiteSpace(database))
                {
                    var sql = new SqlConnectionStringBuilder();
                    sql.DataSource     = server;
                    sql.InitialCatalog = database;

                    if (odbc.ContainsKey(OdbcUidKey))
                    {
                        //Standard Connection
                        sql.IntegratedSecurity = false;
                        sql.UserID             = odbc.GetValue(OdbcUidKey);
                        sql.Password           = odbc.GetValue(OdbcPasswordKey);
                    }
                    else
                    {
                        //Trusted Connection
                        sql.IntegratedSecurity = true;
                    }
                    return(sql.ConnectionString);
                }
            } catch (ArgumentException) { }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Converts ODBC connection string to the SQL Client (.NET).
        /// </summary>
        public static string OdbcToSqlClient(this string odbcString)
        {
            if (string.IsNullOrEmpty(odbcString))
            {
                return(null);
            }
            try {
                var odbc     = new OdbcConnectionStringBuilder(odbcString);
                var server   = odbc.GetValue(OdbcServerKey);
                var database = odbc.GetValue(OdbcDatabaseKey);
                if (!string.IsNullOrWhiteSpace(server) && !string.IsNullOrWhiteSpace(database))
                {
                    var sql = new SqlConnectionStringBuilder {
                        DataSource     = server,
                        InitialCatalog = database,
                    };

                    var userId = odbc.GetValue(OdbcUidKey);
                    if (!string.IsNullOrEmpty(userId))
                    {
                        sql.UserID = odbc.GetValue(OdbcUidKey);
                    }

                    var password = odbc.GetValue(OdbcPasswordKey);
                    if (!string.IsNullOrEmpty(password))
                    {
                        sql.Password = password;
                    }

                    // If no password and user name, assume integrated authentication
                    sql.IntegratedSecurity     = string.IsNullOrEmpty(sql.UserID) && string.IsNullOrEmpty(sql.Password);
                    sql.TrustServerCertificate = string.Compare(odbc.GetValue(OdbcTrustServerCertificateKey), "yes", StringComparison.OrdinalIgnoreCase) == 0;

                    // Translate authentication method
                    if (odbc.ContainsKey(OdbcAuthenticationKey))
                    {
                        SqlAuthenticationMethod authMethod;
                        if (Enum.TryParse(odbc.GetValue(OdbcAuthenticationKey), out authMethod))
                        {
                            sql.Authentication = authMethod;
                        }
                    }

                    return(sql.ConnectionString);
                }
            } catch (ArgumentException) { }
            return(null);
        }
        public static string GetValue(this string odbcString, string key)
        {
            var odbc = new OdbcConnectionStringBuilder(odbcString);

            return(odbc.GetValue(key));
        }