transaction of 2 db operations

  • Thread starter Thread starter tony
  • Start date Start date
T

tony

Hi,

How can i create transaction in c# net compact 2.0 ?

for example :

Execute Command A and Command B as one opertation only.
if B fail, A will not effect the db.

Hope im clear.
 
You have to create a SqlCeTransaction object with the BeginTransaction
method of the SqlCeConnection. Then you have to set the Transaction property
for Command A and Command B to the SqlCeTransaction object and at the end
invoke the Commit method of the SqlCeTransaction object to persist changes,
or Rollback to cancel.

Take a look here:
http://msdn.microsoft.com/en-us/library/csz1c3h7(VS.80).aspx
--

Alberto Silva
www.moving2u.pt - R&D Manager
http://msmvps.com/AlbertoSilva - Blog
Microsoft MVP - Device Application Development
 
For DAAB, you may want to use these extension methods, the first returns a
SqlCeTransaction and some of the others let you execute a command over an
existing transaction, without having to create the command 'by hand'. At the
end you simply have to call the commit or rollback methods over the active
SqlCeTransaction method.

(VB.net)
Module DaabExtensionMethods

<Extension()> _
Public Function BeginTransaction(ByVal daabobject As
Global.Microsoft.Practices.Mobile.DataAccess.SqlDatabase) As
SqlCeTransaction
Return daabobject.GetConnection.BeginTransaction
End Function

<Extension()> _
Public Function PrepareCommand(ByVal daabobject As
Global.Microsoft.Practices.Mobile.DataAccess.SqlDatabase, ByVal commandText
As String, ByVal ParamArray parameters() As SqlCeParameter) As SqlCeCommand
Dim command As New SqlCeCommand
With command
.Connection = daabobject.GetConnection
.CommandText = commandText
.Parameters.AddRange(parameters)
.Prepare()
End With
Return command
End Function

<Extension()> _
Public Function PrepareCommand(ByVal daabobject As
Global.Microsoft.Practices.Mobile.DataAccess.SqlDatabase, ByVal transaction
As SqlCeTransaction, ByVal commandText As String, ByVal ParamArray
parameters() As SqlCeParameter) As SqlCeCommand
Dim command As New SqlCeCommand
With command
.Connection = daabobject.GetConnection
.Transaction = transaction
.CommandText = commandText
.Parameters.AddRange(parameters)
.Prepare()
End With
Return command
End Function

<Extension()> _
Public Function ExecuteNonQuery(ByVal daabobject As
Global.Microsoft.Practices.Mobile.DataAccess.SqlDatabase, ByVal transaction
As SqlCeTransaction, ByVal commandText As String, ByVal ParamArray
parameters() As SqlCeParameter) As Integer
Dim command As SqlCeCommand = daabobject.CreateCommand(transaction,
commandText, parameters)
Dim i As Integer = command.ExecuteNonQuery
command.Dispose()
Return i
End Function

<Extension()> _
Public Function CreateCommand(ByVal daabobject As
Global.Microsoft.Practices.Mobile.DataAccess.SqlDatabase, ByVal commandText
As String, ByVal ParamArray parameters() As SqlCeParameter) As SqlCeCommand
Dim command As New SqlCeCommand
With command
.Connection = daabobject.GetConnection
.CommandText = commandText
.Parameters.AddRange(parameters)
End With
Return command
End Function

<Extension()> _
Public Function CreateCommand(ByVal daabobject As
Global.Microsoft.Practices.Mobile.DataAccess.SqlDatabase, ByVal transaction
As SqlCeTransaction, ByVal commandText As String, ByVal ParamArray
parameters() As SqlCeParameter) As SqlCeCommand
Dim command As New SqlCeCommand
With command
.Connection = daabobject.GetConnection
.Transaction = transaction
.CommandText = commandText
.Parameters.AddRange(parameters)
End With
Return command
End Function
End Module

--

Alberto Silva
www.moving2u.pt - R&D Manager
http://msmvps.com/AlbertoSilva - Blog
Microsoft MVP - Device Application Development
 
Back
Top