2009/04/19

[C#]ConnectionStringBuilder

書き溜めてるのを書いとこうと思って。

会社のC#のバージョンが1.1なので、2.0以降にあるConnectionStringBuilderの真似事をしたもの。

社内用ツール、作業用ツールを作るときに今でも使ってます。

 

ソースはここ

public interface IConnectionStringBuilder
{
    string ConnectionFormat { get; }
    string DriverName { get; }
    string ServerName { get; }
    string DatabaseName { get; }
    string UserId { get; }
    string Password { get; }
    string Timeout { get; }
    string Make ();
}

 

public class ConnectionStringBuilder : IConnectionStringBuilder
{
    #region フィールド
    protected readonly string connnectionFormat;
    protected string driverName;
    protected string serverName;
    protected string databaseName;
    protected string userId;
    protected string password;
    protected int timeout;
    protected string CONNECTION_STRING_NOT_EXIST_MESSAGE = "接続文字列フォーマットが指定されていないか、不正です。";
    protected const string CONNECTION_STRING_FORMAT_ODBC = "Driver={{{5}}};Server={3};UID={0};PWD={1};Database={2};Timeout={4}";
    protected const int DEFAULT_TIMEOUT = 15;
    #endregion

    #region コンストラクタ
    /// <summary>
    /// コンストラクタ
    /// </summary>
    /// <param name="connectionFormatString"></param>
    /// <param name="driverName"></param>
    /// <param name="serverName"></param>
    /// <param name="userId"></param>
    /// <param name="password"></param>
    /// <param name="timeout"></param>
    protected ConnectionStringBuilder(
        string connectionFormatString, string driverName,
        string serverName, string userId, string password, int timeout)
    {
        if (connectionFormatString == null || connectionFormatString == string.Empty)
        {
            throw new NullReferenceException(CONNECTION_STRING_NOT_EXIST_MESSAGE);
        }
        this.connnectionFormat  = connectionFormatString;
        this.driverName = driverName;
        this.serverName = serverName;
        this.userId = userId;
        this.password = password;
        this.timeout = timeout;
    }
    /// <summary>
    /// コンストラクタ
    /// </summary>
    public ConnectionStringBuilder ()
        : this(CONNECTION_STRING_FORMAT_ODBC, string.Empty, string.Empty, string.Empty, string.Empty, DEFAULT_TIMEOUT) { }
    #endregion

    #region IConnectionStringBuilder member
    public string ConnectionFormat
    {
        get
        {
            return this.connnectionFormat;
        }
    }

    public string DriverName
    {
        get
        {
            return this.driverName;
        }
        set
        {
            this.driverName = value;
        }
    }

    public string ServerName
    {
        get
        {
            return this.serverName;
        }

        set
        {
            this.serverName = value;
        }
    }

    public string DatabaseName
    {
        get
        {
            return this.databaseName;
        }

        set
        {
            this.databaseName = value;
        }
    }

    public string UserId
    {
        get
        {
            return this.userId;
        }

        set
        {
            this.userId = value;
        }
    }

    public string Password
    {
        get
        {
            return this.password;
        }

        set
        {
            this.password = value;
        }
    }

    public string Timeout
    {
        get
        {
            return this.timeout.ToString();
        }

        set
        {
            int i = this.ToInt(value);
            this.timeout = i == 0 ? 15 : i;
        }
    }

    /// <summary>
    /// 接続文字列生成
    /// </summary>
    /// <returns></returns>
    public string Make()
    {
        return string.Format(
            this.connnectionFormat
            , this.userId
            , this.password
            , this.databaseName
            , this.serverName
            , this.timeout
            , this.driverName);
    }
    #endregion

    #region インスタンスメソッド
    /// <summary>
    /// 接続文字列を返します。
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        base.ToString ();
        return this.Make();
    }
    private int ToInt(string str)
    {
        if (str == null || str == string.Empty)
        {
            return 0;
        }
        double d;
        double.TryParse(str, System.Globalization.NumberStyles.Any, null, out d);

        return int.Parse(str);
    }
    #endregion
}

0 件のコメント:

コメントを投稿