1、声明连接myConnectString
2、开启事务BeginTransaction
3、执行时使用st
4、成功提交,失败回滚
Public function testshiwu()
Dim strSQL As String
Dim myConnectString As String = o.GetConn
Dim myConnection As New SqlConnection(myConnectString)
myConnection.Open()
Dim st As SqlTransaction = myConnection.BeginTransaction()
Try
'插入用户
strSQL = "Insert into EC_User(cUserID,cUserName,cPassword,cDepCode,badministrator,buse,bDel,cdyzm,cip,cdemo) "
strSQL += "values('test','test','test','01',0,0,0,'t','','')"
SqlHelper.ExecuteNonQuery(st, CommandType.Text, strSQL)
'更新用户
strSQL = "update ec_user set cusername='aaa' where cuserid='test' "
SqlHelper.ExecuteNonQuery(st, CommandType.Text, strSQL)
'再次插入用户,违反主键约束,事务回滚
strSQL = "Insert into EC_User(cUserID,cUserName,cPassword,cDepCode,badministrator,buse,bDel,cdyzm,cip,cdemo) "
strSQL += "values('test','test','test','01',0,0,0,'t','','')"
SqlHelper.ExecuteNonQuery(st, CommandType.Text, strSQL)
st.Commit()
return "success"
Catch ex As Exception
st.Rollback()
MsgBox(ex.Message, MsgBoxStyle.Critical, "发生错误")
Return ""
Finally
myConnection.Close()
End Try
End Sub