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

user139 2002-09-01 10:17:51
不想浪费时间了,请托一个出来。
要求:进度条显示连接数据库过程,连接不成功弹出警告窗口“连接失败”,如连接成功则进入页面下载过程,也用进度条控制,后台下载完后完整显示页面。
分数:100
资金:50
...全文
57 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
因文件超过20M不能上传,所以拆为两个文件次上传 第1章 COM背景知识 1.1 COM的起源 1.1.1 软件业面临的挑战 1.1.2 传统解决方案 1.1.3 面向对象程序设计方法 1.1.4 最终解决方案:组件软件 1.1.5 面向对象的组件模型——COM 1.2 COM的发展历程 1.2.1 COM以前的对象技术:DDE、OLE 1、VBX控件 1.2.2 COM首次亮相:OLE2 1.2.3 Microsoft拥抱Internet:ActiveX 1.2.4 更多的新名词:Windows DNA和COM+ 1.2.5 远程对象:ORBs和DCOM 1.2.6 COM的最新版本:COM+ 1.3 COM技术现状 1.3.1 COM与CORBA 1.3.2 COM与Enterprise Java Beans 1.3.3 Windows之外的COM 小结 第2章 从C++到COM 2.1 C++客户重用C++对象——例程DB 2.1.1 C++对象 2.1.2 客户程序 2.2 将C++对象移进DLL中——例程DB_cppdll 2.2.1 成员函数的引出 2.2.2 内存配 2.2.3 Unicode/ASCII兼容 2.2.4 例程实现 2.2.4.1 修改接口文件 2.2.4.2 修改对象程序 2.2.4.3 修改客户程序 2.3 C++对象使用抽象基类——例程DB_vtbl 2.3.1 问题:私有数据成员被暴露 2.3.2 解决方案:抽象基类 2.3.2.1 什么是抽象基类(Abstract Base Class) 2.3.2.2 实现秘诀:虚函数(Virtual Functions) 2.3.3 使用抽象基类 2.3.4 例程实现 2.3.4.1 修改接口文件 2.3.4.2 修改对象程序 2.3.4.3 修改客户程序 2.4 改由COM库装载C++对象——例程dbalmostcom 2.4.1 COM库 2.4.2 对象创建的标准入口点 2.4.3 标准对象创建API 2.4.4 标准对象注册 2.4.5 例程实现 2.4.5.1 修改接口文件 2.4.5.2 修改对象程序 2.4.5.3 修改客户程序 2.5 将C++对象变成COM对象 2.5.1 引用计数 2.5.2 多接口 2.5.3 IUnknown接口 2.5.4 标准类厂接口:IClassFactory 2.5.5 对象代码的动态卸载 2.5.6 自动注册 2.5.7 例程实现 2.5.7.1 修改接口文件 2.5.7.2 修改对象程序 2.5.7.3 修改客户程序 2.6 为COM对象添加多接口支持 2.6.1 多接口 2.6.2 DEFINE_GUID 2.6.3 例程实现 2.6.3.1 修改接口文件 2.6.3.2 修改对象程序 2.6.3.3 修改客户程序 小结 第3章 COM基础知识 3.1 对象与接口 3.1.1 COM对象 3.1.2 COM接口 3.1.3 IUnknown接口 3.1.3.1 生存期控制:AddRef和Release 3.1.3.2 接口查询:QueryInterface 3.1.4 全球唯一标识符GUID 3.1.5 COM接口定义 3.1.6 接口描述语言IDL 3.2 COM应用模型 3.2.1 客户/服务器模型 3.2.2 进程内组件 3.2.3 进程外组件 3.2.4 COM库 3.2.5 HRESULT返回值 3.2.6 COM与注册表 3.3 COM组件 3.3.1 实现类厂对象 3.3.2 类厂对象的创建 3.3.3 实现自动注册 3.3.4 实现自动卸载 3.4 COM客户 3.4.1 COM对象创建函数 3.4.1.1 CoGetClassObject 3.4.1.2 CoCreateInstance 3.4.1.3 CoCreateInstanceEx 3.4.2 如何调用进程内组件 3.4.3 COM客户调用进程外组件 3.5 进一步认识COM 3.5.1 可重用机制:包容和聚合 3.5.2 进程透明性 3.5.3 安全性机制 小结 第4章 COM扩展技术 4.1 可连接对象机制 4.1.1 客户、接收器与可连接对象 4.1.1.1 接收器 4.1.1.2 可连接对象 4.1.1.3 客户 4.1.2 实现可连接对象 4.1.3 实现接收器 4.1.4 建立接收器与连接点的连接 4.1.5 获得出接口的类型信息 4.2 结构化存储 4.2.1 什么叫结构化存储和复合文件 4.2.2 存储对象和IStorage接口 4.2.2.1 IStorage接口 4.2.2.2 获得IStorage指针 4.2.2.3 释放STATSTG内存 4.2.2.4 枚举存储对象中的元

1,979

社区成员

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

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