如何在C#.net中处理sqlserver的异常 [英] how to handle exceptions of sqlserver in c# .net
本文介绍了如何在C#.net中处理sqlserver的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是迪纳卡.请告诉我在使用c#.net代码连接到sqlserver时如何使用错误号处理sql server的异常...请帮助我
Hi,
This is dinakar. Please tell me how to handle exceptions of sql server using its error number while we are connecting to sqlserver using c#.net code...please help me out
推荐答案
您可以捕获SqlExceptions并检查数字
数字在这里定义
http://msdn.microsoft.com/en-us/library/aa937595%28v = sql.80%29.aspx [ ^ ]
You can catch for SqlExceptions and check the numbers
The numbers are defined here
http://msdn.microsoft.com/en-us/library/aa937595%28v=sql.80%29.aspx[^]
try
{
// Code to access DB
}
catch (SqlException ex)
{
switch (ex.Number)
{
case 2764:
// do something when error 'Incorrect password supplied for application role' is thrown
break;
}
}
如果SQL Server引发异常,则可以捕获该异常,并检查该异常的错误号.如果在存储的proc中出现一个数字,并且您在SQL Server中捕获了该数字,则可以返回错误号.我不确定您还要问什么?
If SQL Server throws an exception, you can catch it, and examine the exception for the error number. If a number occurs within a stored proc and you catch it in SQL Server, you can return the error number. I''m not sure what else you are trying to ask ?
在正在访问它的代码周围放置一个try ... catch块,并检查catch块中的异常.错误的异常没有特别的部分:您必须从异常类型和消息中进行工作.
Put a try...catch block around teh code which is accessing it, and examine the exception in the catch block. There is no specific part of an exception that is the error number: you will have to work from the exception type and message.
try
{
// Code to access DB
}
catch (Exception ex)
{
///Handle error here
}
这篇关于如何在C#.net中处理sqlserver的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文