Forum Moderators: open

Message Too Old, No Replies

How Start transaction in ADO.NET?

         

shd_vb

1:54 pm on Jun 28, 2002 (gmt 0)



We only in the begining of our project.
The forms were designed in VB.NET
and tables were designed in MS SQL Server 2000.
So, how to work with ADO.NET?
the first main thing I should do -
start the transaction, but I didn't find
information in Visual Studio.NET Documentation.
I will appreciate your help.
Thanks
Dima

satanclaus

8:53 pm on Jun 28, 2002 (gmt 0)



I've never worked in .NET but I assume it is similar to 6.0 in most ways??

In VB 6.0 you would...

1. Add the components for Microsoft ADO Data Controls
2. Add an ADODC control to your form
3. Set up the properties.(connectionstring, commandtype, recordsource)
4. Connect your form's controls to the data

Of course this could be completely relevant to .NET but I thought I'd throw it out there.

tomasz

1:24 pm on Jul 4, 2002 (gmt 0)

10+ Year Member



here is the vb.net code example

Public Sub RunSqlTransaction(myConnString As String)
Dim myConnection As New SqlConnection(myConnString)
myConnection.Open()

Dim myCommand As New SqlCommand()
Dim myTrans As SqlTransaction

' Start a local transaction
myTrans = myConnection.BeginTransaction()
' Must assign both transaction object and connection
' to Command object for a pending local transaction
myCommand.Connection = myConnection
myCommand.Transaction = myTrans

Try
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
myCommand.ExecuteNonQuery()
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
myCommand.ExecuteNonQuery()
myTrans.Commit()
Console.WriteLine("Both records are written to database.")
Catch e As Exception
myTrans.Rollback()
Console.WriteLine("Error: {1}", e.Message)
Console.WriteLine("Error reported by {1}.", e.Source)
Console.WriteLine("Neither record was written to database.")
Finally
myConnection.Close()
End Try
End Sub 'RunSqlTransaction