nsegi
Messages postés31Date d'inscriptionmardi 22 juillet 2008StatutMembreDernière intervention20 décembre 2013
-
7 mars 2013 à 13:47
cs_Trim
Messages postés60Date d'inscriptionsamedi 4 mars 2006StatutMembreDernière intervention 4 septembre 2023
-
7 mars 2013 à 17:04
Bonjour à tous,
J'ai une application windows dans laquelle je doit passer deux saisis l'un après l'autre, et mon souci est de pourvoir utiliser la transaction sql pour pouvoir annuler la transaction si première saisi ou la deuxième échoue. voici ce que j'ai put trouvé:
Public Function CreateTransactionScope( _
ByVal connectString1 As String, ByVal connectString2 As String, _
ByVal commandText1 As String, ByVal commandText2 As String) As Integer
' Initialize the return value to zero and create a StringWriter to display results.
Dim returnValue As Integer = 0
Dim writer As System.IO.StringWriter = New System.IO.StringWriter
' Create the TransactionScope in which to execute the commands, guaranteeing
' that both commands will commit or roll back as a single unit of work.
Using scope As New TransactionScope()
Using connection1 As New SqlConnection(connectString1)
Try
' Opening the connection automatically enlists it in the
' TransactionScope as a lightweight transaction.
connection1.Open()
' Create the SqlCommand object and execute the first command.
Dim command1 As SqlCommand = New SqlCommand(commandText1, connection1)
returnValue = command1.ExecuteNonQuery()
writer.WriteLine("Rows to be affected by command1: {0}", returnValue)
' If you get here, this means that command1 succeeded. By nesting
' the Using block for connection2 inside that of connection1, you
' conserve server and network resources by opening connection2
' only when there is a chance that the transaction can commit.
Using connection2 As New SqlConnection(connectString2)
Try
' The transaction is promoted to a full distributed
' transaction when connection2 is opened.
connection2.Open()
' Execute the second command in the second database.
returnValue = 0
Dim command2 As SqlCommand = New SqlCommand(commandText2, connection2)
returnValue = command2.ExecuteNonQuery()
writer.WriteLine("Rows to be affected by command2: {0}", returnValue)
Catch ex As Exception
' Display information that command2 failed.
writer.WriteLine("returnValue for command2: {0}", returnValue)
writer.WriteLine("Exception Message2: {0}", ex.Message)
End Try
End Using
Catch ex As Exception
' Display information that command1 failed.
writer.WriteLine("returnValue for command1: {0}", returnValue)
writer.WriteLine("Exception Message1: {0}", ex.Message)
End Try
End Using
' If an exception has been thrown, Complete will
' not be called and the transaction is rolled back.
scope.Complete()
End Using
' The returnValue is greater than 0 if the transaction committed.
If returnValue > 0 Then
writer.WriteLine("Transaction was committed.")
Else
' You could write additional business logic here, notify the caller by
' throwing a TransactionAbortedException, or log the failure.
writer.WriteLine("Transaction rolled back.")
End If
' Display messages.
Console.WriteLine(writer.ToString())
Return returnValue
' '==========================================utundi
End Function
j'ai une erreur sur: Using scope As New TransactionScope()
cs_Trim
Messages postés60Date d'inscriptionsamedi 4 mars 2006StatutMembreDernière intervention 4 septembre 20233 7 mars 2013 à 17:04
Bonjour,
il faut que tu test "command1.ExecuteNonQuery()", cette commande doit te renvoyer "False" si elle a échoué. Elle ne renvoie pas forcément une chaine de caractère !
Fait une recherche sur ce que te renvoie la fonction ExecuteNonQuery().