违反UNIQUE KEY约束无法在对象中插入重复键

时间:2014-01-27 09:25:33

标签: sql sql-server vb.net

------------------------这是我的sql程序更新表--------------- ---------

create procedure sp_stepUpdate
@ID int,
@StepOrder int

AS
BEGIN  


IF OBJECT_ID('tempdb.dbo.#UpdateBatch','u') IS NOT NULL
begin
DROP TABLE #UpdateBatch
end

IF OBJECT_ID('tempdb.dbo.#UpdateBatch2','u') IS NOT NULL
begin
DROP TABLE #UpdateBatch2
end

create table #UpdateBatch2
(
ID int,
StepOrder int
)

insert into #UpdateBatch2 values (@ID,@StepOrder)

Select *,ROW_NUMBER() OVER(ORDER BY ID) as newIID into #UpdateBatch
from #UpdateBatch2

set identity_insert [ODM_BatchSteps] ON

Update [ODM_BatchSteps]
set [StepOrder] = newIID
From #UpdateBatch 
where [ODM_BatchSteps].ID = #UpdateBatch.ID

set identity_insert [ODM_BatchSteps] off
END
go

---------------这是程序中的代码,用于从列表中获取新订单------

    connection.Open()


    For Each item As ListViewItem In ListView1.Items

        Dim command As SqlCommand = New SqlCommand("sp_stepUpdate", connection)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.AddWithValue("@ID", item.SubItems(0).Text)
        command.Parameters.AddWithValue("@StepOrder", item.SubItems(1).Text)
        command.ExecuteNonQuery()
    Next

当我尝试使用listview中的新订单更新表格时,我违反了UNIQUE KEY约束

-----------这是我试图更新的订单-----

    create table [dbo].[ODM_BatchSteps]
(
[ID] uniqueidentifier primary key not null default newid(),
[StepOrder]int ,
[StepType]int,
[StepGrid]nvarchar(max),
[BatchGrid]int,
foreign key (BatchGrid) REFERENCES ODM_Batches(ID)
)

3 个答案:

答案 0 :(得分:1)

错误意味着您正在尝试插入数据库中已存在的键值(id?)。 我只看到一个插入语句,所以你最好检查一下你传递给它的值..

答案 1 :(得分:1)

我认为你的字段BatchGrid标识了一组按特定顺序保存的记录 如果是这种情况并且没有外键引用您的ODM_BatchSteps字段,那么正确重写此记录块的一种粗鲁但有效的方法是删除引用相同BatchGrid的每个条目。然后重新插入ListView项目中的所有内容

Dim tran as SqlTransaction
Try
    connection.Open()
    tran = connection.BeginTransaction()
    Dim command As SqlCommand = new SqlCommand("DELETE FROM ODM_BatchSteps WHERE BatchGrid = @grd", connection, tran)
    command.Parameters.AddWithValue("@grd", currentGrid)
    command.ExecuteNonQuery()
    For Each item As ListViewItem In ListView1.Items

        ' Now we INSERT every item in the grid passing the parameters 
        ' required to rebuild the block of records for the same BatchGrid
        command = New SqlCommand("usp_stepInsert", connection, tran)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.AddWithValue("@ID", item.SubItems(0).Text)
        command.Parameters.AddWithValue("@StepOrder", item.SubItems(1).Text)
        command.Parameters.AddWithValue("add the other parameters to rebuild the record")
        command.ExecuteNonQuery()
    Next
    tran.Commit()
Catch Ex as Exception
    ' Log the exception, message to user ???
    tran.RollBack
End Try

当然,您的sp_stepUpdate应该重命名并重写(usp_stepInsert?),以接受以正确的步骤顺序插入新记录所需的所有参数

如果这是一种可行的方法,那么您可以尝试使用Table Valued Parameter来提升性能,而不是为每个项目单独调用数据库

答案 2 :(得分:1)

好的感谢帮助人们非常感谢...采取了简单的方法并添加了计数以添加新订单的行数,将其替换为新表中数据库中的旧StepOrder

-------------------- sql update ---------------------

Update [ODM_BatchSteps]
set ODM_BatchSteps.StepOrder = UpdateBatch2.StepOrder
From UpdateBatch2 
where [ODM_BatchSteps].ID = UpdateBatch2.ID

----------------程序中的代码---------------------

Dim count As Integer

    For Each item As ListViewItem In ListView1.Items
        count = count + 1
        Dim command As SqlCommand = New SqlCommand("sp_stepUpdate", connection)
        command.CommandType = CommandType.StoredProcedure

        command.Parameters.AddWithValue("@ID", item.SubItems(0).Text)
        command.Parameters.AddWithValue("@StepOrder", count)
        command.Parameters.AddWithValue("@StepType", item.SubItems(2).Text)
        command.Parameters.AddWithValue("@StepGrid", item.SubItems(3).Text)
        command.Parameters.AddWithValue("@BatchGrid", item.SubItems(4).Text)        
        command.ExecuteNonQuery()

    Next


        connection.Close()