如何解决“无法启用约束”的问题。一行或多行包含违反非空,唯一或外键约束的值。错误 [英] how to solve the problem of "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." error

查看:153
本文介绍了如何解决“无法启用约束”的问题。一行或多行包含违反非空,唯一或外键约束的值。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用该函数时

when i call the function

public DataTable GetAllBySurSurveyId(Int64 surSurveyId)
        {
            DataTable dtSurSurveyTeam = null;
            try
            {
                dtSurSurveyTeam = Adapter.sp_4_15_GetAllBySurSurveyId(surSurveyId);
            }
            catch (Exception ex)
            {
                ErrorWriter.WriteError(ex, "FunctionName --> GetDataTable", RequestPage);
            }
            return dtSurSurveyTeam;
        }





上述错误来了。我用storeprocedure绑定了quary,storeprocedure显示了数据。



storeprocedure:





the above error came. i have binded the quary with storeprocedure and the storeprocedure is showing the data.

storeprocedure :

ALTER PROCEDURE [dbo].[sp_4_15_GetAllBySurSurveyId] 
	-- Add the parameters for the stored procedure here
	@surSurveyId AS nvarchar(MAX)
AS
BEGIN

DECLARE @Sql1 AS NVARCHAR(MAX)

SET @Sql1 =
'SELECT        T.SurSurveyTeamId, O.OfficeName, T.SurveyTeamName, T.TeamDescription,
                             (SELECT        COUNT(SurSurveyTeamMemberId) AS TotalMembers
                               FROM            SurRelationSurveyTeamToTeamMember AS RTM
                               WHERE        (SurSurveyTeamId = R.SurSurveyTeamId)) AS TotalMembers
FROM            SurRelationSurveyToSurveyTeam AS R LEFT OUTER JOIN
                         SurSurveyTeam AS T ON T.SurSurveyTeamId = R.SurSurveyTeamId LEFT OUTER JOIN
                         UsmOffice AS O ON O.UsmOfficeId = T.UsmOfficeId
WHERE        (R.SurSurveyId = '+@surSurveyId+')'
 
EXECUTE (@Sql1)	
END

推荐答案

正如错误信息所示,约束类型您可能违反的是非null,唯一或外键约束。除非您自己添加它们,否则它们只能在DataTable和DataSet架构由数据源等自动生成时创建。



此问题通常由



- >未设置为AllowDBNull的列返回空值

- >使用相同的主键返回重复的行。

- >数据库与数据集之间的列定义(例如字段大小)不匹配
As the error message says, the types of constraints that you might violate are non-null, unique, or foreign-key constraints. Unless you add them yourself, they can only be created when DataTable and DataSet schema is generated automatically, by the Data Source or the like.

This problem is usually caused by

-> null values being returned for columns not set to AllowDBNull
-> duplicate rows being returned with the same primary key.
-> a mismatch in column definition (e.g. size of char fields) between the database and the dataset


这篇关于如何解决“无法启用约束”的问题。一行或多行包含违反非空,唯一或外键约束的值。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆