问题描述
我想在UI中获取的错误消息中获得以下异常中的所有替换值,以便显示自定义消息到细粒度级别
I want to obtain all the replaced values in the following exception from error message obtained on UI to display custom message up to a granular level
%ls语句与%ls约束冲突%1!。冲突发生在数据库%。* ls中,表%。* ls%ls%。* ls%ls。
The %ls statement conflicted with the %ls constraint "%.*ls". The conflict occurred in database "%.*ls", table "%.*ls"%ls%.*ls%ls.
我可以观察
%ls
正在被替换为UPDATE
, FOREIGN KEY
等。
在C#中,我将收到错误消息,我想从中提取部分替换为SQL Server。我将在我的库中维护 SqlErrors
,并将其替换为自定义消息。
In C# I will be getting error message from which I want to extract the parts replaced by SQL Server. I will be maintaining SqlErrors
in my library and will replace them with custom messages.
自定义消息我想显示
例如
Custom message I want to displayeg.
string.Format("{0} failed!, incorrect value was supplied for {1} field", "UPDATE", "Status")
推荐答案
SQLException有属性,您可以使用该号码然后翻译成任何
SQLException has Number property, you can use this number then translate to whatever text you want.
public static string GetSqlExceptionMessage(int number)
{
//set default value which is the generic exception message
string error = MyConfiguration.Texts.GetString(ExceptionKeys.DalExceptionOccured);
switch (number)
{
case 4060:
// Invalid Database
error = MyConfiguration.Texts.GetString(ExceptionKeys.DalFailedToConnectToTheDB);
break;
case 18456:
// Login Failed
error = MyConfiguration.Texts.GetString(ExceptionKeys.DalFailedToLogin);
break;
case 547:
// ForeignKey Violation
error = MyConfiguration.Texts.GetString(ExceptionKeys.DalFKViolation);
break;
case 2627:
// Unique Index/Constriant Violation
error = MyConfiguration.Texts.GetString(ExceptionKeys.DalUniqueConstraintViolation);
break;
case 2601:
// Unique Index/Constriant Violation
error =MyConfiguration.Texts.GetString(ExceptionKeys.DalUniqueConstraintViolation);
break;
default:
// throw a general DAL Exception
MyConfiguration.Texts.GetString(ExceptionKeys.DalExceptionOccured);
break;
}
return error;
}
示例代码从:
这篇关于如何操作SqlException消息到用户友好的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!