【问题标题】:How to validate ConnectionString exists and if not do not throw error如何验证 ConnectionString 是否存在,如果不存在则不抛出错误
【发布时间】:2011-07-06 16:03:30
【问题描述】:

在应用程序配置中未找到连接名称“MySqlServer”或连接字符串为空。

所以,我有一个带有面板的页面,当找到 Web 配置中的连接并且连接有效时,该面板将显示;使用 try/catch 只要添加名称“VALUE”在配置连接字符串中,如果服务器数据错误,页面将加载并且面板设置为不可见......我需要能够处理以下内容。 ..

如果本例中的命名值MySqlServer用在aspx中; aspx.cs 但在配置中找不到我不希望发生错误;未找到连接名称....我只想不显示面板,就像 SqlConnection.Open 在找到名称但数据错误时失败...

aspx

   <asp:SqlDataSource runat="server" ID="allowedIPsSqlDataSource" 
    ConnectionString="<%$ ConnectionStrings:MySqlServer %>"

aspx.cs

string connectionString = ConfigurationManager.ConnectionStrings["MySqlServer"].ToString();
        SqlConnection SqlConnection = new SqlConnection(connectionString);
        SqlCommand SqlCommand = new SqlCommand();
        try
        {
            SqlConnection.Open();

配置

<connectionStrings>
        <add name="NotMySqlServer" providerName="System.Data.SqlClient" connectionString="server=TEST\SQL2005;database=ADB;Integrated Security=True"/>
<add name="NotMy2SqlServer" providerName="System.Data.SqlClient" connectionString="server=TEST\SQL2005;database=ADB;Integrated Security=True"/>

    </connectionStrings>

【问题讨论】:

    标签: c# connection-string validation


    【解决方案1】:

    你可以试试:

    if(ConfigurationManager.ConnectionStrings["MySqlServer"] == null) throw ...
    

    【讨论】:

    • 我认为最好检查一下:string.IsNullOrWhiteSpace(the_connection_string)
    【解决方案2】:

    如果您使用的是 .NET 4.5+ 并且可以访问 C# 6.0,则可以使用 null 条件运算符 (?) 来尝试获取连接字符串,而不会自动引发异常:

    string connectionString = ConfigurationManager.ConnectionStrings["MySqlServer"]?.ConnectionString;
    //------------------------------------------------------------------------HERE-^-HERE-------------
    
    if (string.IsNullOrWhiteSpace(connectionString))
    {
        // Don't even bother trying to open the connection.
        // Log the error and either rethrow the exception (throw;) or exit from your current context (return;).
        //return;
        //throw;
    }
    
    // If your code has made it this far, it means you have a valid connection string.  Now try to use it.
    using (var sqlConnection = new SqlConnection(connectionString))
    {
        sqlConnection.Open();
    
        using (var sqlCommand = new SqlCommand)
        {
            // Do stuff.
        }
    }
    

    【讨论】:

    【解决方案3】:

    您可以使用count检查是否有任何连接字符串。

    var count = ConfigurationManager.ConnectionStrings.Count;
    if (count > 0)
    {
        //There is at least more then one connection string.
    }
    

    更新

    public static class Extension
    {
        public static bool HasConnectionString(this ConnectionStringSettingsCollection value, string key)
    {
        try
        {
            return value[key].ConnectionString.Length > 0;
        }catch 
        {
            return false;
        }
    }
    }
    

    您可以按如下方式使用扩展程序。

    if (ConfigurationManager.ConnectionStrings.HasConnectionString("MySqlServer"))
    {
        //If true you know there is a valid connectionstring.
    }
    

    【讨论】:

    • ConnectionString "name" = 'MySqlServer" 时如何具体计算
    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2016-05-01
    • 1970-01-01
    • 2015-04-30
    • 2016-01-28
    相关资源
    最近更新 更多