blogjava's web log

blogjava's web log
...

验证数据库链接字符串

当我正要找的时候,竟然就让我碰到了下面这段代码。运气不错

/// <summary>
/// Checks a connection string for validity
/// </summary>
/// <param name="cn">SQL Connection string to validate</param>
/// <returns>True if connection is valid.</returns>
/// <history>
///     [Tim Hibbard]   12/03/2006  Created
/// </history>
public static bool IsValidSQLConnectionString(string cn)
{
    bool RV = true;
    try
    {
        if (!cn.Contains("Data Source"))
        {
            throw new MissingMemberException("Missing Data Source");
        }
        if (!cn.Contains("Initial Catalog"))
        {
            throw new MissingMemberException("Missing Initial Catalog");
        }
        if (!cn.Contains("User ID"))
        {
            throw new MissingMemberException("Missing User ID");
        }
        if (!cn.Contains("Password"))
        {
            throw new MissingMemberException("Missing Password");
        }
        string[] contents = cn.Split(char.Parse(";"));
        if (!contents[0].StartsWith("Data Source"))
        {
            throw new MissingMemberException("Missing Data Source");
        }
        if (!contents[1].StartsWith("Initial Catalog"))
        {
            throw new MissingMemberException("Missing Initial Catalog");
        }
        if (!contents[2].StartsWith("User ID"))
        {
            throw new MissingMemberException("Missing User ID");
        }
        if (!contents[3].StartsWith("Password"))
        {
            throw new MissingMemberException("Missing Password");
        }
    }
    catch (Exception)
    {
        RV = false;
    }
    return RV;
}
Update to SQL connection string validation code

posted on 2007-05-12 21:37 record java and net 阅读(392) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论