如何找到违反约束的列?

时间:2014-11-03 09:07:15

标签: c# sql-server-2008 stored-procedures strongly-typed-dataset

我有一个强类型数据集,它会为空值抛出此错误,

  

System.Data.ConstraintException:无法启用约束。一个或   更多行包含违反非null,唯一或外键的值   限制。在System.Data.DataTable.EnableConstraints()处   System.Data.DataTable.EndLoadData()at   System.Data.Common.DataAdapter.FillFromReader(DataSet数据集,   DataTable数据表,String srcTable,DataReaderContainer dataReader,   Int32 startRecord,Int32 maxRecords,DataColumn parentChapterColumn,   对象parentChapterValue)at   System.Data.Common.DataAdapter.Fill(DataTable [] dataTables,   IDataReader dataReader,Int32 startRecord,Int32 maxRecords)at   System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,   DataTable [] datatables,Int32 startRecord,Int32 maxRecords,String   srcTable,IDbCommand命令,CommandBehavior行为)at   System.Data.Common.DbDataAdapter.Fill(DataTable [] dataTables,Int32   startRecord,Int32 maxRecords,IDbCommand命令,CommandBehavior   在System.Data.Common.DbDataAdapter.Fill(DataTable   dataTable)at   Fruits.DataAccess.FruitsTableAdapters.FruitsExtTableAdapter.GetFruits(字符串   用户,字符串过滤器)at   Fruits.DataAccess.FruitsDataAccess.GetFruits(String User,String   过滤)Fruits.WebServices.External.Fruity.GetFruits(String   过滤器)

所有列都填充了1行,我正在使用

进行测试
USE [FruitDataBase]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Fruits](
    [ID] [int] NOT NULL,
    [CategoryID] [int] NOT NULL,
    [Title] [nvarchar](255) NOT NULL,
    [URL] [nvarchar](255) NOT NULL,
    [Status] [nvarchar](70) NOT NULL,
    [Description] [nvarchar](1024) NULL,
    [User1] [nvarchar](50) NOT NULL,
    [Date] [datetime] NOT NULL,
    [User2] [nvarchar](50) NULL,
    [Date2] [datetime] NULL,
    [Impact] [nvarchar](255) NULL,
    [Solution] [nvarchar](1024) NULL,
 CONSTRAINT [PK_Fruits] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Fruits]  WITH CHECK ADD  CONSTRAINT [FK_Fruits_Categories] FOREIGN KEY([CategoryID])
REFERENCES [dbo].[Categories] ([ID])
GO

ALTER TABLE [dbo].[Fruits] CHECK CONSTRAINT [FK_Fruits_Categories]
GO

4 个答案:

答案 0 :(得分:19)

DataSet / DataTable具有可获取有关错误的更多详细信息的属性。

所以这些细节并不像你想象的那样在异常中,这就是诀窍。见http://www.codeproject.com/Tips/405938/Debugging-DataSet-Constraint-Errors

示例:

catch (ConstraintException)
{
    DataRow[] rowErrors = this.YourDataSet.YourDataTable.GetErrors();

    System.Diagnostics.Debug.WriteLine("YourDataTable Errors:" 
        + rowErrors.Length);

    for (int i = 0; i < rowErrors.Length; i++)
    {
        System.Diagnostics.Debug.WriteLine(rowErrors[i].RowError);

        foreach (DataColumn col in rowErrors[i].GetColumnsInError())
        {
            System.Diagnostics.Debug.WriteLine(col.ColumnName 
                + ":" + rowErrors[i].GetColumnError(col));
        }
    }
}

答案 1 :(得分:1)

问题在于其中一个列名在数据集中不匹配,不确定是什么触发了它,但匹配的列名与一个返回修复了问题。

我还使用过&#34;预览数据&#34;强类型数据集的特征,帮助我找出问题。

答案 2 :(得分:1)

您可以在代码中使用此方法。 .NET会检查数据集,从而抛出异常。

       public void CheckDataSet(DataSet dataSet)
       {                                                              
        Assembly assembly = Assembly.LoadFrom(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll");
        Type type = assembly.GetType("System.Data.ConstraintEnumerator");
        ConstructorInfo ctor = type.GetConstructor(new[] { typeof(DataSet) });
        object instance = ctor.Invoke(new object[] { dataSet });                
        BindingFlags bf = BindingFlags.Instance | BindingFlags.Public;
        MethodInfo m_GetNext = type.GetMethod("GetNext", bf);

        while ((bool)m_GetNext.Invoke(instance, null))
        {
            bool flag = false;
            MethodInfo m_GetConstraint = type.GetMethod("GetConstraint", bf);                    
            Constraint constraint = (Constraint) m_GetConstraint.Invoke(instance, null);
            Type constraintType = constraint.GetType();
            BindingFlags bfInternal = BindingFlags.Instance | BindingFlags.NonPublic;
            MethodInfo m_IsConstraintViolated = constraintType.GetMethod("IsConstraintViolated", bfInternal);                    
            flag = (bool)m_IsConstraintViolated.Invoke(constraint, null);
            if (flag)                    
                Debug.WriteLine("Constraint violated, ConstraintName: " + constraint.ConstraintName + ", tableName: " + constraint.Table);                                            
        }

        foreach (DataTable table in dataSet.Tables)
        {
            foreach (DataColumn column in table.Columns)
            {
                Type columnType = column.GetType();
                BindingFlags bfInternal = BindingFlags.Instance | BindingFlags.NonPublic;

                bool flag = false;
                if (!column.AllowDBNull)
                {                            
                    MethodInfo m_IsNotAllowDBNullViolated = columnType.GetMethod("IsNotAllowDBNullViolated", bfInternal);                                                        
                    flag = (bool)m_IsNotAllowDBNullViolated.Invoke(column, null);
                    if (flag)
                    {
                        Debug.WriteLine("DBnull violated  --> ColumnName: " + column.ColumnName + ", tableName: " + column.Table.TableName);
                    }
                }
                if (column.MaxLength >= 0)
                {
                    MethodInfo m_IsMaxLengthViolated = columnType.GetMethod("IsMaxLengthViolated", bfInternal);                            
                    flag = (bool)m_IsMaxLengthViolated.Invoke(column, null);                            
                    if (flag)                            
                        Debug.WriteLine("MaxLength violated --> ColumnName: " + column.ColumnName + ", tableName: " + column.Table.TableName);
                }
            }
        }                                                    
}

答案 3 :(得分:0)

仅在某些情况下(例如目前与我一样)使用数据集在VB.NET中处理旧项目,以节省您一分钟的时间,我将在@AFract答案旁发布VB.NET版本。希望它适合这里:

        Catch ex As ConstraintException
            Dim rowErrors = this.YourDataSet.YourDataTable.GetErrors()
            System.Diagnostics.Debug.WriteLine("YourDataTable Errors:" & rowErrors.Length)
            For i = 0 To rowErrors.Length - 1
                For Each col As DataColumn In rowErrors(i).GetColumnsInError()
                    System.Diagnostics.Debug.WriteLine(col.ColumnName & ":" & rowErrors(i).GetColumnError(col))
                Next
            Next
        End Try