One Form Multiple Tables

L

LacOniC

I have one form, should insert data to multiple tables in same database.
Can u give me an example?
 
G

Guest

Hi Dear LacOniC,

What Controls you have on your form? Is it a webform, winform?

But In anycase, what you can do is , write code like this

Dim myConnection As New SqlConnection(myConnString)
myConnection.Open()

Dim myCommand As SqlCommand = myConnection.CreateCommand()
Dim myTrans As SqlTransaction

' Start a local transaction

myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted,
"SampleTransaction")

' 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 Table1(Field1, Field2,.....)
VALUES ('" & textbox1.text.tostring() & "', '" & textbox2.text.tostring() &
"')"
myCommand.ExecuteNonQuery()

myCommand.CommandText = "Insert into Table2(Field1, Field2,.....)
VALUES ('" & textbox1.text.tostring() & "', '" & textbox2.text.tostring() &
"')"
myCommand.ExecuteNonQuery()

myTrans.Commit()

Console.WriteLine("Both records are written to database.")

Catch e As Exception
Try
myTrans.Rollback("SampleTransaction")
Catch ex As SqlException

If Not myTrans.Connection Is Nothing Then
Console.WriteLine("An exception of type " &
ex.GetType().ToString() & _
" was encountered while attempting to roll back
the transaction.")

End If

End Try

Console.WriteLine("An exception of type " & e.GetType().ToString() & _
"was encountered while inserting the data.")
Console.WriteLine("Neither record was written to database.")

Finally

myConnection.Close()

End Try

End Sub 'RunSqlTransaction

For Anything & Everything, Please Let Me Know,

Bye
Venkat_KL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,797
Messages
2,569,647
Members
45,377
Latest member
Zebacus

Latest Threads

Top