哪位给个数据库连接事务例程(100分完全送)

user139 2002-09-01 10:17:51
不想浪费时间了,请托一个出来。
要求:进度条显示连接数据库过程,连接不成功弹出警告窗口“连接失败”,如连接成功则进入页面下载过程,也用进度条控制,后台下载完后完整显示页面。
分数:100
资金:50
...全文
50 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lbx1979 2002-09-11
  • 打赏
  • 举报
回复
你这个是数据库连接吗?
给你贴片文章看看吧
Creating transactional ASP.NET pages

Introduction
Many time our business rules require that a certain set of operations be performed in a ‘batch’. The batch is atomic i.e. if any of the operation from the batch fails all the operations are undone. To achieve such functionality we frequently use transactions. Transactions offer ACID properties (Atomicity, Consistency, Isolation and Durability). In ASP.NET application transactions can be implemented at two levels:

At component level : In this case you will write your code that handles transactions and marks the transaction as successful or fail. You will host such components in MTS or COM+.
At page level : It might the case that you are not using components in your ASP.NET pages. You can still use the transactional behavior by marking the ASP.NET page to support transaction. ASP.NET also raises certain page level events that are fired after transaction succeeds or fails.
In this article we will see how to handle transactions in ASP.NET page itself.

Marking an ASP.NET page as Transactional
ASP.NET has a page level attribute called Transaction which is used to mark that page as transactional. Following code fragment will make the use of Transaction attribute clear :

<%@ Page Language="VB" Transaction="Required" %>
Once you mark the page as transactional, all the operations within that page take place in a single transaction. You can commit or rollback the operations depending on some business logic or other errors.

Importing Required Namespaces
Once you declare a page as transactional you can decide whether to mark the transaction as successful (COMMIT) or failure (ABORT). In ASP.NET you make use of a special namespace for this purpose. The namespace is Microsoft.ComServices. You need to set reference to assembly Microsoft.ComServices.dll and import the namespace in your page as follows:

<%@ Assembly Name="Microsoft.ComServices.dll %><%@ Import Namespace="Microsoft.ComServices" %>
Note : If you are using Visual Studio.NET you can right click on references folder in solution explorer and all a reference to the assembly.

Marking a transaction as successful or fail

Now that you have declared your page as transactional and imported required namespaces, you can perform any operations and decide whether to mark the transaction as successful or fail. To do this you use ContextUtil class provided by Microsoft.ComServices namespace. The class has following static methods :

SetComplete : This method marks a transaction as successful and all the changes are committed to the database.
SetAbort : This method marks the transaction as fail and any changes are undone.
Following code fragment makes use of these methods clear :

Dim cnn as ADOConnectionDim cmd as ADOCommandCnn=new ADOConnection(myconnectionstring)Cnn.Open()Cmd=new ADOCommand()cmd.ActiveConnection=cnnTry cmd.CommandText="INSERT INTO Account_A VALUES(1001,’25000’)" cmd.ExecuteNonQuery() cmd.CommandText="DELETE FROM Account_B WHERE ID=1001" cmd.ExecuteNonQuery() ContextUtil.SetComplete()Catch e as Exception ContextUtil.SetAbort()End Try
Here, we are inserting a record in Account_A table and at the same time deleting the same record from Account_B table. We want that the row should be deleted from Account_B table only if it has successfully inserted in Account_A table also, the row should get inserted in Account_A table only if it gets deleted from Account_B table. We therefore put the statements in try-catch block for any errors. If there are no errors we COMMIT the transaction so that changes are permanently written to the database. If any error occur during execution of any of the statement we ABORT the transaction so that database state is unaffected.

Transaction Events
You may want to display some error message when transaction is committed or aborted or you may want to log the information in some log file. ASP.NET page provides two page level events that are fired when a transaction is committed or aborted. They are :

Page_AbortTransaction
Page_CommitTransaction
As the names suggest AbortTransaction event is fired when you call ContextUtil.SetAbort method. Similarly, CommitTransaction event is fired when ContextUtil.SetComplete method is called. Here is how the events look like:

Public Sub Page_AbortTransaction
(sender as object, e as EventArgs) Response.write("Transaction Failed")End Sub
Public Sub Page_CommitTransaction
(sender as object, e as EventArgs) Response.write("Transaction Successful")End Sub

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧