Exemplo n.º 1
0
        public static string GetDatabasePassword(this DbConnectionStringBuilder builder, DataAccessProviderType?providerType = null)
        {
            if (providerType == DataAccessProviderType.OleDb ||
                IsOleDb(builder))
            {
                return(builder.TryGetValue("Jet OLEDB:Database Password", out var value)
                    ? (string)value
                    : null);
            }

            // MAJOR ISSUE: PWD seems to be used for the datbase password AND the workgroup user password.
            //              See https://stackoverflow.com/questions/65025810/how-to-specify-mdw-username-and-password-user-level-security-and-the-database
            //              As a workaround, we will assume the PWD field to contain the database password, if no SYSTEMDB has been specified.
            //              Otherwise, we will handle the field as the workgroup user password.

            if (providerType == DataAccessProviderType.Odbc ||
                IsOdbc(builder))
            {
                return(string.IsNullOrEmpty(builder.GetSystemDatabase(providerType))
                    ? builder.TryGetValue("PWD", out var value)
                        ? (string)value
                        : null
                    : null);
            }

            return(builder.TryGetValue("Jet OLEDB:Database Password", out var oleDbValue)
                ? (string)oleDbValue
                : string.IsNullOrEmpty(builder.GetSystemDatabase(providerType))
                    ? builder.TryGetValue("PWD", out var odbcValue)
                        ? (string)odbcValue
                        : null
                    : null);
        }
        private static string GetOleDbConnectionString(string fileNameOrConnectionString)
        {
            if (JetStoreDatabaseHandling.IsConnectionString(fileNameOrConnectionString) &&
                JetConnection.GetDataAccessProviderType(fileNameOrConnectionString) == DataAccessProviderType.OleDb)
            {
                return(fileNameOrConnectionString);
            }

            var filePath         = JetStoreDatabaseHandling.ExpandFileName(JetStoreDatabaseHandling.ExtractFileNameFromConnectionString(fileNameOrConnectionString));
            var connectionString = JetConnection.GetConnectionString(filePath, DataAccessProviderType.OleDb);

            if (JetStoreDatabaseHandling.IsConnectionString(fileNameOrConnectionString) &&
                JetConnection.GetDataAccessProviderType(fileNameOrConnectionString) == DataAccessProviderType.Odbc)
            {
                var oldCsb = new DbConnectionStringBuilder(true)
                {
                    ConnectionString = fileNameOrConnectionString
                };
                var newCsb = new DbConnectionStringBuilder {
                    ConnectionString = connectionString
                };

                newCsb.SetUserId(oldCsb.GetUserId(DataAccessProviderType.Odbc), DataAccessProviderType.OleDb);
                newCsb.SetPassword(oldCsb.GetPassword(DataAccessProviderType.Odbc), DataAccessProviderType.OleDb);
                newCsb.SetSystemDatabase(oldCsb.GetSystemDatabase(DataAccessProviderType.Odbc), DataAccessProviderType.OleDb);
                newCsb.SetDatabasePassword(oldCsb.GetDatabasePassword(DataAccessProviderType.Odbc), DataAccessProviderType.OleDb);

                connectionString = newCsb.ConnectionString;
            }

            return(connectionString);
        }