如何从错误消息中提取路径? [英] How to extract a path from error message?

查看:63
本文介绍了如何从错误消息中提取路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建数据库的SQL查询,但是由于该文件存在于其实例文件夹中,因此无法创建它。这一切都很好。

形成错误信息,我想提取特定的路径。



任何Suggesstions ??



代码:

 静态  void  Main( string  [] args)
{

string userSpecifiedDBNameInTxtBox;
string globalConnection = uid = sa;密码= ABC;服务器= XYZ;数据库=主人;

SqlConnection conn = new SqlConnection(globalConnection);

Console.WriteLine( 输入要创建的数据库名称) ;
userSpecifiedDBNameInTxtBox = Console.ReadLine();

string cmdtext = String .Format( CREATE DATABASE {0},userSpecifiedDBNameInTxtBox);

SqlCommand cmd = new SqlCommand(cmdtext,conn);
conn.Open();
尝试
{

cmd.ExecuteNonQuery();
}
catch (SqlException错误)
{
if (err.Number == 5170
{
字符串 message = err.Message;

FunctionToRenameTheDatabaseInPhysicalDrive();

}
}

解决方案

您可以使用正则表达式从错误字符串中提取文件路径。 Google是您的朋友 [ ^ ]。

I have a sql query that creates a Database but gives exception that it cannot be created because the file exists in it's instance folder. This is all fine.
Form the error message, i wanted to extract the particular path.

Any Suggesstions??

Code:

static void Main(string[] args)
       {

           string userSpecifiedDBNameInTxtBox;
           string globalConnection = "uid=sa;password=abc;server=XYZ;database=master";

           SqlConnection conn = new SqlConnection(globalConnection);

           Console.WriteLine("Enter the database name to be created");
           userSpecifiedDBNameInTxtBox= Console.ReadLine();

           string cmdtext =String.Format( "CREATE DATABASE {0}",userSpecifiedDBNameInTxtBox);

           SqlCommand cmd = new SqlCommand(cmdtext, conn);
           conn.Open();
           try
           {

               cmd.ExecuteNonQuery();
           }
           catch (SqlException err)
           {
               if (err.Number == 5170)
               {
                  string message= err.Message;

                   FunctionToRenameTheDatabaseInPhysicalDrive();
               }
           }

解决方案

You may use a regular expression for extracting the file path from the error string. Google is your friend[^].


这篇关于如何从错误消息中提取路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆