Billa
Billa

Reputation: 5266

C# string match based on wildcards

I am getting some database exception like

Database 'db_name' cannot be opened. It is in the middle of a restore

try
{
}
catch(Exception ex)
{
  if(ex.Message.Contains("Database 'db_name' cannot be opened. It is in the middle of a restore")){
   //show user friendly message
  }
}

But the problem is we have around 10-15 database names and we will be keep adding more.

How do i match a string that has a wildcard?

Is there any regex to match something like Database '{whatevercomes}' cannot be opened. It is in the middle of a restore

Upvotes: 0

Views: 294

Answers (4)

Tim Schmelter
Tim Schmelter

Reputation: 460098

You should handle only the SqlException instead of all exceptions. Then differentiate between the errors via the Number property. Your error is 927.

Here you find all: http://technet.microsoft.com/en-us/library/aa937592(v=sql.80).aspx

try
{
    // ...
}
catch(SqlException ex)
{
    if(ex.Number == 927)
    {
        //show user friendly message
    }
}

According to the actual name of the database: It seems not to be possible to retrieve it from the SqlException. Since the message could also change due to localization, why can't you handle this exception where you've opened the connection? Then you could simply use the SqlConnection.DataBase-property.

For example:

using (var con = new SqlConnection("Connection-String"))
{
    try
    {
        con.Open();
        // ...
    } catch (SqlException ex)
    {
        string database = con.Database;
        if (ex.Number == 927)
        {
            //show user friendly message
            string errorMessage = string.Format("Could not open database '{0}' since it's currently restoring. Please inform your database administrator."
                                               , database);
            MessageBox.Show( errorMessage );
        }
    }
}

Upvotes: 3

Rico Suter
Rico Suter

Reputation: 11858

Try this regex: Database '(.*?)' cannot be opened\. It is in the middle of a restore

Usage:

var regex = @"Database '(.*?)' cannot be opened\. It is in the middle of a restore";
var match = Regex.Match(msg, regex);
if (match .Success)
{
    var databaseName = match.Groups[1].Value; 
    // Show messgage
}

But you should not parse the exception text but only look at the exception type and the Number property as mentioned in another question. (Reason: What if your application is executed on an operation system with another language?)

Upvotes: 1

Agent_Spock
Agent_Spock

Reputation: 1177

instead of regex just use this

 if(ex.Message.IndexOf("Database ") == 0 && ex.Message.Contains(" cannot be opened. It is in the middle of a restore") == true)
 {
      //user friendly message here
 }

Upvotes: 1

MJK
MJK

Reputation: 3514

Not sure it will help you or not. Instead of trying regex, what if you use two separate condition to achieve that

try
{
}
catch(Exception ex)
{
  if(ex.Message.StartsWith("Database") && ex.Message.Contains(" cannot be opened. It is in the middle of a restore")){
   //show user friendly message
  }
}

Upvotes: 0

Related Questions