急救:如何在以下的代码中实现事务
RT,最好在VB.NET中用代码实现事务的提交和回滚,谢谢
For i = 0 To 100
strSql = "insert into table1 values( ' " + i.ToString + " ', ' "
strSql = strSql + Trim(cmbvisacode.Text()) + " ', ' "
strSql = strSql + Trim(txtvn.Text()) + " ', ' "
strSql = strSql + Trim(cmbcomcode.Text()) + " ', ' "
strSql = strSql + Now() + " ', ' "
strSql = strSql + user + " ', '10000 ') "
If executeNonQuery(strSql) = True Then
Else
showMsg( "发生错误,请重新操作。 ", MsgBoxStyle.Exclamation)
End If
Next
[解决办法]
Dim connStr As String = "连接数据库语句 "
Dim con As New SqlConnection(connStr)
con.Open()
Dim com As New SqlCommand()
Dim myTrans As SqlTransaction
myTrans = con.BeginTransaction()
com.Connection = con
com.Transaction = myTrans
Try
For i = 0 To 100
strSql = "insert into table1 values( ' " + i.ToString + " ', ' "
strSql = strSql + Trim(cmbvisacode.Text()) + " ', ' "
strSql = strSql + Trim(txtvn.Text()) + " ', ' "
strSql = strSql + Trim(cmbcomcode.Text()) + " ', ' "
strSql = strSql + Now() + " ', ' "
strSql = strSql + user + " ', '10000 ') "
com.CommandText = strSql
com.ExecuteNonQuery()
Next
myTrans.Commit()
MsgBox( "成功! ")
Catch ex As Exception
myTrans.Rollback()
MsgBox( "失败! ")
Finally
con.Close()
End Try