唯一索引异常 [英] Unique index exception

查看:380
本文介绍了唯一索引异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图捕获系统生成的唯一键异常,并且想抛出自己的消息.

我正在Visual Studio 2008中进行操作

我正在使用以下代码.在我即将插入的应用程序代码中,我正在使用此代码.

Hi,

I am trying to catch the system generated unique key exception and I want to throw my own message.

I am doing it in Visual Studio 2008

I am using the following code. In my application code just before inserting i am using this code.

Try
            l_Database.gp_BeginTransaction()
            ls_SqlStr = "SELECT * FROM  mst_country where cn_sid = po_Country.cn_sid"

        Catch ex As SqlException
            Throw ex
        Finally
            l_Database.gp_CloseConnection()
            l_Database = Nothing
        End Try



在我的业务逻辑层中,我试图通过调用此异常来编写自己的消息.

但是我不确定我做得是否正确.

请任何人建议我解决此问题并有效地工作.

问候
Srikar



And in my business logic layer I am trying to write my own message by calling this exception.

But I am not sure whether I am doing correctly or not.

Please can any body suggest me to fix this and work efficiently.

Regards
Srikar

推荐答案



请避免抓住并扔掉它.
如果您发现任何逻辑,那就没问题了.
只是捕获并抛出异常并不会添加任何值
而且太贵了..

而是考虑以下代码:

试试
l_Database.gp_BeginTransaction()
ls_SqlStr =选择*来自mst_country,其中cn_sid = po_Country.cn_sid"

终于
l_Database.gp_CloseConnection()
l_Database =没什么
结束尝试

在这里,catch块被删除,并且在业务逻辑层中进行处理..

您的业​​务逻辑层如下所示:

试试
{
//调用上面的函数.
}
catch(SqlException ex)
{
bool hasOtherErrors = false;
//批处理语句中会包含错误,并且有可能
//您将在其中收到其他错误,例如foriegn key.在这种情况下,您应该抛出
//如果不是唯一的常量错误,则返回错误.
foreach(ex.Errors中的SqlError er)
{
//解析每个sqlerror,并确定其是否是唯一的约束错误.
if(er.ErrorNumber == UNIQUE_CONSTRAINT_ERROR_NO)
{
//在此处显示您的自定义错误消息....
}
其他
{
//如果除UniqueConstraint错误之外还有其他错误..
hasOtherErrors = true;
}
}//每个

if(hasOtherErrors)
{
//抛出错误,因为bathc语句中还有其他错误
//还使用throw来保留堆栈跟踪
投掷;
}
}//cathc
Hi,

Please avoid catching and throwing it.
If you any logics inside catch, then its fine.
Just catching and throwing the exception back doesn''t add a value
and tis expensive too..

Instead think of the following code:

Try
l_Database.gp_BeginTransaction()
ls_SqlStr = "SELECT * FROM mst_country where cn_sid = po_Country.cn_sid"

Finally
l_Database.gp_CloseConnection()
l_Database = Nothing
End Try

Here the catch block is removed and the same is handled in the business logic layer..

Your business logic layer looks like:

try
{
//call the funtion above..
}
catch(SqlException ex)
{
bool hasOtherErrors = false;
//The batch statments will have a collection of errors and there are possibilites
//where you will get other errors like foriegn key..In that case you should throw the
//error back if its not a unique constratint error.
foreach(SqlError er in ex.Errors)
{
//parse each and every sqlerror and identify if its a unique contraint error.
if(er.ErrorNumber == UNIQUE_CONSTRAINT_ERROR_NO)
{
//show your customized error message here....
}
else
{
//If it has any other error other than a UniqueConstraint error..
hasOtherErrors = true;
}
}//for each

if(hasOtherErrors)
{
//rethrow the error since we have other errors in the bathc statement
//Also use throw to preserve stack trace
throw;
}
}//cathc


这篇关于唯一索引异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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