I
Issac
Hi,
I am writing a module which will update 2 tables
(Master/Detail) in SQL database. There's a foreign key
protection among those tables so that I have to add a
record in Master table prior to add record in Detail
table. What I want to do is I want to create a
SqlTansaction to rollback Master insertion whenever
there's error occur in Detail insertion.
Here is my code...
\\\
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim myTrans As SqlTransaction
Dim i As Integer
Dim sqlCnn As New SqlConnection(myConnString)
Try
sqlCnn.Open()
myTrans = sqlCnn.BeginTransaction
For i = 1 To aSqlCmd.GetUpperBound(0)
aSqlCmd(i).Connection = sqlCnn
aSqlCmd(i).Transaction = myTrans
da = New SqlDataAdapter(aSqlCmd(i))
ds = New DataSet
da.Fill(ds)
Next
myTrans.Commit()
Catch sqlEx As SqlException
MsgBox(sqlEx.Message)
myTrans.Rollback()
Catch ex As Exception
MsgBox(ex.Message)
myTrans.Rollback()
Finally
sqlCnn.Close()
End Try
///
What I did is I take an array of SqlCommand(), execute one
by one, then rollback all if there's any error.
However, when it goes to second line, I got a sql error
saying there's a foreign key violation.
I did try to make such insertions in Query Analyzer and
wrap all with Begin Trans, both Commit and Rollback works
fine.
Can I do such batch update with .Net or I did something
wrong?
Please help, thanks.
I am writing a module which will update 2 tables
(Master/Detail) in SQL database. There's a foreign key
protection among those tables so that I have to add a
record in Master table prior to add record in Detail
table. What I want to do is I want to create a
SqlTansaction to rollback Master insertion whenever
there's error occur in Detail insertion.
Here is my code...
\\\
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim myTrans As SqlTransaction
Dim i As Integer
Dim sqlCnn As New SqlConnection(myConnString)
Try
sqlCnn.Open()
myTrans = sqlCnn.BeginTransaction
For i = 1 To aSqlCmd.GetUpperBound(0)
aSqlCmd(i).Connection = sqlCnn
aSqlCmd(i).Transaction = myTrans
da = New SqlDataAdapter(aSqlCmd(i))
ds = New DataSet
da.Fill(ds)
Next
myTrans.Commit()
Catch sqlEx As SqlException
MsgBox(sqlEx.Message)
myTrans.Rollback()
Catch ex As Exception
MsgBox(ex.Message)
myTrans.Rollback()
Finally
sqlCnn.Close()
End Try
///
What I did is I take an array of SqlCommand(), execute one
by one, then rollback all if there's any error.
However, when it goes to second line, I got a sql error
saying there's a foreign key violation.
I did try to make such insertions in Query Analyzer and
wrap all with Begin Trans, both Commit and Rollback works
fine.
Can I do such batch update with .Net or I did something
wrong?
Please help, thanks.