我有一個強類型數據集,其引發此錯誤空值,如何找到違反約束的列?
System.Data.ConstraintException:未能啓用約束。一個或多個行包含違反非空,唯一或外鍵約束的值。在System.Data.DataTable.EnableConstraints()處 System.Data.Common.DataAdapter.FillFromReader System.Data.DataTable.EndLoadData()(數據集的數據集,數據表 數據表,字符串srcTable要,DataReaderContainer的DataReader, 的Int32 startRecord, INT32最大記錄,的DataColumn parentChapterColumn, 對象parentChapterValue)在 System.Data.Common.DataAdapter.Fill(數據表[]數據表, IDataReader的DataReader的,INT32 startRecord,INT32最大記錄)在 System.Data.Common.DbDataAdapter.FillInternal(數據集數據集, DataTable [] datatables,Int32 startRecord,Int32 maxRecords,字符串 srcTable,IDbCommand命令,CommandBehavior行爲)在 System.Data.Com mon.DbDataAdapter.Fill(數據表[]數據表,的Int32 startRecord,的Int32最大記錄,IDbCommand的命令,的CommandBehavior 行爲)在System.Data.Common.DbDataAdapter.Fill(數據表 的dataTable)在 Fruits.DataAccess.FruitsTableAdapters.FruitsExtTableAdapter。 GetFruits(字符串 用戶,字符串濾波器)在 Fruits.DataAccess.FruitsDataAccess.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
簡短回答:沒有 你可以用動態SQL做一些壞事,但我不會建議它。 – Tristan 2014-11-03 09:26:42
謝謝@Tristan,我現在改變了我的問題 – Mathematics 2014-11-03 09:32:03
嘗試'SELECT f。* FROM dbo.Fruits AS f WHERE NOT EXISTS(SELECT 1 FROM dbo.Categories AS c WHERE c.id = f.categoryID);'找到哪些行違反FK約束。 – 2014-11-03 09:51:23