fluent-nhibernate icon indicating copy to clipboard operation
fluent-nhibernate copied to clipboard

Connection to MS Access database with database password

Open dittodhole opened this issue 10 years ago • 0 comments

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?

dittodhole avatar Apr 29 '15 12:04 dittodhole