请看下面的代码:
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
有没有办法在每个循环上提交事务,而不是等到所有记录都循环完,或者这是不好的做法?
我花了一些时间谷歌搜索这个。我认为我不必发布问题,但我还没有找到答案。