我有抛出这个错误空值的强类型数据集,
System.Data.ConstraintException:未能启用约束。一个或多个行包含违反非空,唯一或外键约束。在System.Data.DataTable.EnableConstraints()在System.Data.DataTable.EndLoadData()在System.Data.Common.DataAdapter.FillFromReader(数据集的数据集,数据表的数据表,字符串srcTable要,DataReaderContainer的DataReader,的Int32 startRecord,的Int32最大记录,的DataColumn parentChapterColumn,对象parentChapterValue)在System.Data.Common.DataAdapter.Fill(数据表[]数据表,IDataReader的DataReader的,的Int32 startRecord,的Int32最大记录)在System.Data.Common.DbDataAdapter.FillInternal(数据集的数据集,数据表[]数据表,的Int32 startRecord,的Int32最大记录,字符串srcTable要,IDbCommand的命令,在的CommandBehavior行为System.Data.Common.DbDataAdapter.Fill)(数据表[]数据表,的Int32 startRecord,的Int32最大记录,IDbCommand的命令,的CommandBehavior行为)在System.Data.Common.DbDataAdapter .fill伪(数据表的dataTable)处Fruits.DataAccess.FruitsDataAccess.GetFruits Fruits.DataAccess.FruitsTableAdapters.FruitsExtTableAdapter.GetFruits(字符串用户,字符串滤波器)(字符串用户在Fruits.WebServices.External.Fruity.GetFruits字符串滤波器)(字符串滤波器)
所有列都填充了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
你可以在你的代码中使用此方法。 .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);
}
}
}
}
的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));
}
}
}
问题是与数据集中不匹配的列名的一个,不知道是什么引起的,但有一个固定的归国问题列的匹配名称。
我还使用强类型数据集这让我找出问题的“预览数据”功能。
就在有人的情况下(比如我目前)在VB.NET传统项目与数据集进行操作,以节省您的家伙一分钟,我发布一个版本VB.NET关闭@AFract答案。希望它适合在这里:
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