如何识别从SQL Server 2008中的错误code中的主键重复? [英] How to Identify the primary key duplication from a SQL Server 2008 error code?
问题描述
我想知道我们是如何从SQL Server错误code确定在C#中的主键重复错误。
I want to know how we identify the primary key duplication error from SQL Server error code in C#.
作为一个例子,我有一个C#的形式将数据输入到SQL Server数据库中,而数据输入,我怎么能确定从异常?
As a example, I have a C# form to enter data into a SQL Server database, when an error occurs while data entry, how can I identify the reason for the error from the exception?
推荐答案
如果你赶上<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.aspx\">SqlException再看看它的数字,这个数字 2627
将意味着违反唯一约束(包括主键)。
If you catch SqlException then see its number, the number 2627
would mean violation of unique constraint (including primary key).
try
{
//insertion code
}
catch(SqlException ex)
{
if(ex.Number == 2627)
{
//Violation of primary key. Handle Exception
}
}
这是一个可以不管是否提高常规错误
数据库复制。在复制的数据库,错误的是
通常提出,因为主键还没有被整个拓扑管理得当
This is a general error that can be raised regardless of whether a database is replicated. In replicated databases, the error is typically raised because primary keys have not been managed appropriately across the topology.
这篇关于如何识别从SQL Server 2008中的错误code中的主键重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!