请查看以下代码:
Public Function Orphand()
Dim objCon As SqlConnection
Dim objCommand As SqlCommand
Dim objCommand2 As SqlCommand
Dim objCommand3 As SqlCommand
Dim objDR As SqlDataReader
Dim transaction As SqlTransaction
Try
objCon = New SqlConnection(_ConString)
objCommand = New SqlCommand
objCommand2 = New SqlCommand
objCommand3 = New SqlCommand
objCommand.Connection = objCon
objCommand2.Connection = objCon
objCommand3.Connection = objCon
objCon.Open()
transaction = objCon.BeginTransaction
objCommand.Transaction = transaction
objCommand2.Transaction = transaction
objCommand3.Transaction = transaction
Using objCon
objCommand.CommandText = "SELECT TOP 3 * FROM dbReviews"
objDR = objCommand.ExecuteReader
Do While objDR.Read
objCommand2.CommandText = "INSERT INTO dbReviewChangesVALUES (1,1,'1',1,'2012-01-09')"
objCommand2.ExecuteNonQuery()
objCommand3.CommandText = "INSERT INTO dbReviewChangesVALUES (1,1,'1',1,'2012-01-09')"
objCommand3.ExecuteNonQuery()
Loop
transaction.Commit()
objDR.Close()
End Using
Catch ex As Exception
End Try
End Function
End Class
有没有办法在每个循环上提交事务,而不是等到所有记录循环或这是一种不好的做法?
我花了一些时间谷歌搜索这个。我不认为我会发一个问题,但我没有找到答案。
答案 0 :(得分:1)
是的,在你的循环中打开一个事务。