开发者

How to validate ConnectionString exists and if not do not throw error

The connection name 'MySqlServer' was not found in the applications configuration or the connection string is empty.

So, I have a page with a panel that will display when the connection in the web config is found and the connection is valid; using a try/catch as long as the add name"VALUE" is in the config connection strings if the server data is bad the page will load and the panel is set to invisible... I need to be able to handle the following...

If the named value in this case MySqlServer is used in the aspx; aspx.cs but not found in the config I do not want the error to occur; connection name was not found.... I just want to not show the panel like when the SqlConnection.Open fails when the name is found but data is bad...

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();

config

<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"/>

    </con开发者_运维技巧nectionStrings>


You can try :

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


If you're using .NET 4.5+ and have access to C# 6.0, you can make use of the null conditional operator (?) to try and get the connection string without automatically throwing an exception:

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.
    }
}


You can check if there are any connections strings by using count.

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

Update

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

You can use the extension as follow.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜