C# tableadapter约束失败
我在一个大型项目中使用tableadapter,每次调用任何查询时都会出现“无法启用约束。一行或多行包含违反非null、唯一或外键约束的值”错误。我没有空值,并且在结果集中有唯一的主键值。然而,我已经尝试在生成的源代码中的任何地方将EnforceConstraints设置为False。我还使用该列上下文菜单中的“删除键”选项删除了所有主键。我为tableadapter中的所有列设置AllowDBNull true。异常仍然发生。我在tableadapter下定义了三个查询,所有查询都返回完全相同的列(SQL只在使用的过滤器表达式中有所不同)。通过在线研究这个问题,我发现了在生成的源代码中设置try/catch来检查错误以确定确切原因的建议。当我这样做时,根本不会产生任何错误。生成的源如下所示:C# tableadapter约束失败,c#,sql,exception,constraints,tableadapter,C#,Sql,Exception,Constraints,Tableadapter,我在一个大型项目中使用tableadapter,每次调用任何查询时都会出现“无法启用约束。一行或多行包含违反非null、唯一或外键约束的值”错误。我没有空值,并且在结果集中有唯一的主键值。然而,我已经尝试在生成的源代码中的任何地方将EnforceConstraints设置为False。我还使用该列上下文菜单中的“删除键”选项删除了所有主键。我为tableadapter中的所有列设置AllowDBNull true。异常仍然发生。我在tableadapter下定义了三个查询,所有查询都返回完全相同
public virtual CyberevToo.ReactionImagesDataTable AllMedia(string MindfileID, string FilePath) {
this.Adapter.SelectCommand = this.CommandCollection[1];
if ((MindfileID == null)) {
throw new global::System.ArgumentNullException("MindfileID");
}
else {
this.Adapter.SelectCommand.Parameters[0].Value = ((string)(MindfileID));
}
if ((FilePath == null)) {
throw new global::System.ArgumentNullException("FilePath");
}
else {
this.Adapter.SelectCommand.Parameters[1].Value = ((string)(FilePath));
}
CyberevToo.ReactionImagesDataTable dataTable = new CyberevToo.ReactionImagesDataTable();
this.Adapter.Fill(dataTable);
return dataTable;
}
public virtual CyberevToo.ReactionImagesDataTable AllMedia(string MindfileID, string FilePath) {
this.Adapter.SelectCommand = this.CommandCollection[1];
if ((MindfileID == null)) {
throw new global::System.ArgumentNullException("MindfileID");
}
else {
this.Adapter.SelectCommand.Parameters[0].Value = ((string)(MindfileID));
}
if ((FilePath == null)) {
throw new global::System.ArgumentNullException("FilePath");
}
else {
this.Adapter.SelectCommand.Parameters[1].Value = ((string)(FilePath));
}
CyberevToo.ReactionImagesDataTable dataTable = new CyberevToo.ReactionImagesDataTable();
try
{
this.Adapter.Fill(dataTable);
}
catch (System.Exception ex) {
System.Console.WriteLine(ex.Message);
}
return dataTable;
}
异常在“this.Adapter.Fill(dataTable);”行中抛出。修改后的源代码如下:
public virtual CyberevToo.ReactionImagesDataTable AllMedia(string MindfileID, string FilePath) {
this.Adapter.SelectCommand = this.CommandCollection[1];
if ((MindfileID == null)) {
throw new global::System.ArgumentNullException("MindfileID");
}
else {
this.Adapter.SelectCommand.Parameters[0].Value = ((string)(MindfileID));
}
if ((FilePath == null)) {
throw new global::System.ArgumentNullException("FilePath");
}
else {
this.Adapter.SelectCommand.Parameters[1].Value = ((string)(FilePath));
}
CyberevToo.ReactionImagesDataTable dataTable = new CyberevToo.ReactionImagesDataTable();
this.Adapter.Fill(dataTable);
return dataTable;
}
public virtual CyberevToo.ReactionImagesDataTable AllMedia(string MindfileID, string FilePath) {
this.Adapter.SelectCommand = this.CommandCollection[1];
if ((MindfileID == null)) {
throw new global::System.ArgumentNullException("MindfileID");
}
else {
this.Adapter.SelectCommand.Parameters[0].Value = ((string)(MindfileID));
}
if ((FilePath == null)) {
throw new global::System.ArgumentNullException("FilePath");
}
else {
this.Adapter.SelectCommand.Parameters[1].Value = ((string)(FilePath));
}
CyberevToo.ReactionImagesDataTable dataTable = new CyberevToo.ReactionImagesDataTable();
try
{
this.Adapter.Fill(dataTable);
}
catch (System.Exception ex) {
System.Console.WriteLine(ex.Message);
}
return dataTable;
}
在“System.Console.WriteLine(ex.Message);”行上设置断点,该断点永远不会到达。try/catch就绪后,DataTable返回的数据很好,并且充满了数据。没有它,就不会返回任何数据。当我使用try/catch构建并运行程序时,不会发生异常。当它在没有try/catch的情况下执行时,会失败,出现上述异常
你知道为什么吗?我不想每次修改其中一个定义时都要为XSD中的所有20多个TableAdapter重新编码生成的代码 欢迎来到类型化数据集的世界。也就是说,浪费时间。99%的情况下,结果集中会有一个丢失/错误的列……或者在xsd中不允许有null的null。如何调试?创建一个“视图”,通过SomeKey从dbo.YourActualTable ORDER中选择前100个*。从一个较高的数字“选择前100名”…然后如果仍然失败…将其缩小(如“选择前50名”)…如果有效,则缩小,如果失败,则缩小。继续缩小直到你找到神奇的一排…看看它有什么不同。可能是空的。显然,使用视图填充DataSet/DataTable。没有空值,只要有try/catch,DataSet就可以显示完整的100多行。sql server management studio结果与数据集结果完全匹配,行对行,列对列。