由于fk约束导致sql异常,原因不明

时间:2012-09-05 13:55:41

标签: sql vb.net tsql stored-procedures

所以我得到的确切错误信息是:

  

INSERT语句与FOREIGN KEY约束冲突   “FK_featuredtype_featured”。冲突发生在数据库中   “docphin”,表“dbo.featured”,列“featuredID”。该声明   已被终止。“

我的vb代码中调用具有insert语句的sp的部分是:

If isChanged1.Checked Then
    lq.admin_RemoveFeatured(isChanged.featuredID1)
    lq.admin_AddFeatured(title1.Text, text1.Text, imageURL1.Text, login1.Checked, index1.Checked, mobile1.Checked, Integer.Parse(priority1.Text))

End If

If isChanged2.Checked Then
    lq.admin_RemoveFeatured(isChanged.featuredID2)
    lq.admin_AddFeatured(title2.Text, text2.Text, imageURL2.Text, login2.Checked, index2.Checked, mobile2.Checked, Integer.Parse(priority2.Text))

End If

现在奇怪的是,当我在sql server中执行admin_AddFeatured时,它工作正常。

admin_RemoveFeatured:

CREATE PROCEDURE [dbo].[admin_RemoveFeatured] 
    -- Add the parameters for the stored procedure here
    @featuredID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    delete from featuredtype where featuredID= @featuredID
    delete from featured where featuredID= @featuredID


END

GO

admin_AddFeatured:

CREATE PROCEDURE [dbo].[admin_AddFeatured] 
    -- Add the parameters for the stored procedure here
    @title varchar(500) , 
    @text varchar(MAX),
    @imageURL varchar(200),
    @loginPage bit,
    @indexPage bit,
    @mobilePage bit,
    @priority int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

         -- Insert statements for procedure here
        insert into featured
        (title,text,imageURL, priority )
        values
        (@title,@text,@imageURL, @priority)

        insert into featuredtype
        (loginPage, indexPage, mobilePage)
        values
        (@loginPage, @indexPage, @mobilePage)


END

GO

我一直在测试不同的解决方案,但我似乎无法理解这里可能出现的问题。我唯一的想法是,ic可能与我如何获取我正在插入删除的每个“功能”项目的ID字段有关。为此,我制作了一个模块:

Public Module isChanged
    Public featuredID1 As Integer
    Public featuredID2 As Integer
    Public featuredID3 As Integer
    Public featuredID4 As Integer
    Public featuredID5 As Integer
    Public featuredID6 As Integer
End Module

然后,在页面加载子中,我使用sp并读入ID,如:

   Dim lq2 As New lqDFDataContext
        Dim var = lq2.admin_GetFeatured().ToList()
        Dim i As Integer = 1

        For Each f In var
            If i = 1 Then
                isChanged.featuredID1 = f.featuredID
                title1.Text = f.title
                text1.Text = f.text
                imageURL1.Text = f.imageURL
                login1.Checked = f.loginPage
                index1.Checked = f.indexPage
                mobile1.Checked = f.mobilePage
                priority1.Text = Str(f.priority)
            End If

etc...

1 个答案:

答案 0 :(得分:3)

您没有在featuredID

中设置featuredType

试试这个

   select @featuredID = Scope_Identity()

   insert into featuredtype 
        (loginPage, indexPage, mobilePage,featuredID) 
        values 
        (@loginPage, @indexPage, @mobilePage, @featuredID)