Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection to MS Access database with database password #310

Open
dittodhole opened this issue Apr 29, 2015 · 0 comments
Open

Connection to MS Access database with database password #310

dittodhole opened this issue Apr 29, 2015 · 0 comments

Comments

@dittodhole
Copy link

I tried to use the following construct and expected that the connectionString will contain a Jet OLEDB:Database Password-section

JetDriverConfiguration.Standard.ConnectionString(arg => arg.DatabaseFile("foo")
                                                           .Password("foo"));

This was not the case, so I've implemented two classes to extend the JetDriverConfiguration-scenario by replacing it like so:

public sealed class AccessConnectionStringBuilder : ConnectionStringBuilder
{
    private string databaseFile;
    private string databasePassword;
    private string password;
    private string provider;
    private string username;

    public AccessConnectionStringBuilder()
    {
        this.provider = "Microsoft.Jet.OLEDB.4.0";
    }

    public AccessConnectionStringBuilder Provider(string provider)
    {
        this.provider = provider;
        this.IsDirty = true;
        return this;
    }

    public AccessConnectionStringBuilder DatabaseFile(string databaseFile)
    {
        this.databaseFile = databaseFile;
        this.IsDirty = true;
        return this;
    }

    public AccessConnectionStringBuilder Username(string username)
    {
        this.username = username;
        this.IsDirty = true;
        return this;
    }

    public AccessConnectionStringBuilder Password(string password)
    {
        this.password = password;
        this.IsDirty = true;
        return this;
    }

    public AccessConnectionStringBuilder DatabasePassword(string databasePassword)
    {
        this.databasePassword = databasePassword;
        this.IsDirty = true;
        return this;
    }

    protected override string Create()
    {
        var str = base.Create();
        if (!string.IsNullOrEmpty(str))
        {
            return str;
        }
        var stringBuilder = new StringBuilder();
        if (!string.IsNullOrEmpty(this.databasePassword))
        {
            stringBuilder.AppendFormat("Provider={0};Data Source={1};Jet OLEDB:Database Password={2};",
                                       this.provider,
                                       this.databaseFile,
                                       this.databasePassword);
        }
        else if (!string.IsNullOrEmpty(this.password))
        {
            stringBuilder.AppendFormat("Provider={0};Data Source={1};User Id={2};Password={3};",
                                       this.provider,
                                       this.databaseFile,
                                       this.username,
                                       this.password);
        }
        else
        {
            stringBuilder.AppendFormat("Provider={0};Data Source={1}",
                                       this.provider,
                                       this.databaseFile);
        }

        return stringBuilder.ToString();
    }
}


public class AccessDriverConfiguration : PersistenceConfiguration<AccessDriverConfiguration, AccessConnectionStringBuilder>
{
    protected AccessDriverConfiguration()
    {
        this.Dialect<JetDialect>();
        this.Driver<JetDriver>();
    }

    public static AccessDriverConfiguration Standard
    {
        get
        {
            return new AccessDriverConfiguration();
        }
    }
}

Is this a legit replacement, or is there anything alike in your library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant