———————— voici ma procédure sql pour mettre à jour la table —————— ——
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
————— et voici mon code dans le programme pour get la nouvelle command de la list ——
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
je reçois une violation de la contrainte UNIQUE KEY lorsque j'essaie de mettre à jour la table avec la nouvelle command de ma list
———– voici la table l'ordre iam essayant de mettre à jour —–
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) )
L'erreur signifie que vous essayez d'insert une valeur de key (id?) Qui existe déjà dans la database. Je vois seulement une instruction d'insertion, donc vous feriez mieux de vérifier quelles valeurs vous lui passez.
Je suppose que votre champ BatchGrid
identifie un groupe d'loggings à conserver dans un ordre particulier.
Si c'est le cas et qu'il n'y a pas de foreign keys qui se réfèrent à vos champs ODM_BatchSteps
, un moyen grossier, mais efficace pour réécrire correctement ce bloc d'loggings consiste à supprimer chaque input qui fait reference au même BatchGrid
, puis réinsert tout à partir de vos éléments 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
Bien sûr, votre sp_stepUpdate devrait être renommé et réécrit ( usp_stepInsert
?) Pour accepter tous les parameters requirejs pour INSÉRER un nouvel logging dans l'ordre correct
Si cette approche est viable, vous pouvez essayer d'augmenter les performances à l'aide d'un paramètre Table Valued au lieu de faire un appel distinct à la database pour chaque élément.
Ok merci pour l'aide que les gars ont beaucoup appréciée … a pris la sortie facile et a ajouté le count pour append quelle ligne de numéro la nouvelle command est, l'a remplacé par l'ancien StepOrder dans la DB à partir d'une nouvelle table
——————– sql update ———————
Update [ODM_BatchSteps] set ODM_BatchSteps.StepOrder = UpdateBatch2.StepOrder From UpdateBatch2 where [ODM_BatchSteps].ID = UpdateBatch2.ID
—————- code dans le programme ———————
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()