大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> 用Microsoft.net实现数据库事务(二)

用Microsoft.net实现数据库事务(二)

时间: 2021-07-31 作者:daque

数据库工作数据库工作是其余工作模子的普通,正文计划的工作都是鉴于数据库工作实行的,她们仅是湮没了处置工作的搀杂的大概容器私有的代码。当一个工作创造时各别数据库体例都有本人的准则。缺省级地区级,sql server处事在机动提交的形式下,每个语句实行完后会登时提交,与此比较的是oracle须要你包括一个提交语句。然而当一个语句经过oledb实行时,它实行完后一个提交举措会被附加上去。为了使底下的例子实用于oracle,你须要去掉begin transation和commit transation两个语句,由于这两个语句会看成一个独立的工作来运转。上风l 一切的工作论理包括在一个独立的挪用中l 具有运转一个工作的最好本能l 独力于运用步调控制l 工作左右文仅生存于数据库挪用中l 数据库代码与数据库体例相关例子:create procedure up_purchaseitem ( @customerid as int, @itemid int, @itemqty int)asdeclare @orderid intbegin transaction-- update the inventory based on purchaseupdate inventoryset qtyinstock = qtyinstock - @itemqtywhere inventory.productid = @itemid;if @@error != 0 goto error_handler-- insert the order into the databaseinsert into ordersvalues (@customerid, @itemid, @itemqty, getdate());if @@error != 0 goto error_handlerset @orderid = @@identitycommit transactionreturn @orderiderror_handler: rollback transaction set nocount off return 0goado.net工作创造一个ado.net工作是很大略的,只是是规范代码的一个小的扩充。只有你领会怎样运用ado.net来考察数据库,那就差不离领会了。辨别只是是你须要把代码放到一个工作左右文中。仍旧从来的ado.net类库援用,在实行工作的类内里引入system.data和system.data.sqlclient类库,为了实行一个工作,你须要创造一个sqltransation东西,不妨挪用你的sqlconnection东西begintransation()本领来创造它,一旦你把sqltransation东西存为当地变量,你就不妨把它赋给你的sqlcommand东西的工作属性,大概把它动作结构器的一个参数来创造sqlcommand。在实行sqlcommand举措之前,你必需挪用begintransaction()本领,而后赋给sqlcommand工作属性。一单工作发端了,你就不妨实行任何度数的sqlcommand举措,只有它是属于同一个工作和贯穿。结果你不妨挪用sqltransation的commit()本领来提交工作。ado.net工作本质上是把工作左右文传播到数据库层,即使工作中爆发一个缺点,数据库会机动回滚。在你的缺点处置代码中,历次挪用rollback()本领之前查看工作东西能否生存是一种杰出的风气。如许的一个例子是当一个死锁爆发的同声,数据库正在实行机动回滚。上风:l 大略性l 和数据库工作差不离的快l 工作不妨超过多个数据库考察l 独力于数据库,各别数据库的私有代码被湮没了控制:工作实行在数据库贯穿层上,以是你须要在工作进程中手动的保护一个贯穿例子:public int purchaseitem(int customerid, int itemid, int itemqty) { sqlconnection con = null; sqltransaction tx = null; int orderid = 0; try { con = new sqlconnection("data source=localhost; user id=sa;password=;initial catalog=trans_db;"); con.open(); tx = con.begintransaction(isolationlevel.serializable); string updatesqltext = "update inventory set qtyinstock = qtyinstock - " + itemqty.tostring() + " where inventory.productid = " + itemid.tostring(); sqlcommand cmd = new sqlcommand(updatesqltext, con, tx); cmd.executenonquery(); // string is 2 sql statements: the first is the insert, the second selects the identity column string insertsqltext = "insert into orders values (" + customerid.tostring() + "," + itemid.tostring() + "," + itemqty.tostring() + " , getdate() ); select @@identity"; cmd.commandtext = insertsqltext; // retrieve the order id from the identity column orderid = convert.toint32(cmd.executescalar()); cmd.dispose(); tx.commit(); } catch (sqlexception sqlex) { // specific catch for deadlock if (sqlex.number != 1205) { tx.rollback(); } orderid = 0; throw(sqlex); } catch (exception ex) { tx.rollback(); orderid = 0; throw (ex); } finally { tx.dispose(); con.close(); }}asp.net工作asp.net工作不妨说是在.net平台上工作实行办法中最大略的一种,你只是须要加一条龙代码。在aspx的页面证明中加一个特殊的属性,即是工作属性,它不妨有 如次的值:disabled (缺省), notsupported, supported, required 和 requiresnew,那些树立和com+以及企业级效劳中的树立一律,典范地即使你想在页面左右文中运转工作,那么要树立为required。即使页面中包括有效户控件,那么那些控件也会包括到工作中,工作会生存于页面包车型的士每个场合。上风:l 实行大略,不须要特殊的源代码控制:l 页面包车型的士一切代码都是同一个工作,如许的工作大概会很大,而大概咱们须要的是划分的、小的工作l 工作简直web层例子:aspx page<%@ page transaction="required" language="c#" codebehind="aspnet_transaction.aspx.cs" inherits="transtest.aspnet_transaction" autoeventwireup="false"%><html> <head> <title>asp.net transaction</title> </head> <body ms_positioning="gridlayout"> <form id="aspnet_transaction" method="post" runat="server"> </form> </body></html>aspx code behind pageusing system;using system.collections;using system.componentmodel;using system.data;using system.data.sqlclient;using system.drawing;using system.web;using system.web.sessionstate;using system.web.ui;using system.web.ui.webcontrols;using system.web.ui.htmlcontrols;namespace transtest{ /// summary description for aspnet_transaction. public class aspnet_transaction : system.web.ui.page { public int purchaseitem(int customerid, int itemid, int itemqty) { sqlconnection con = null; int orderid = 0; try { con = new sqlconnection("data source=localhost; user id=sa;password=;initial catalog=trans_db;"); con.open(); string updatesqltext = "update inventory set qtyinstock = qtyinstock - " + itemqty.tostring() + " where inventory.productid = " + itemid.tostring(); sqlcommand cmd = new sqlcommand(updatesqltext, con); cmd.executenonquery(); string insertsqltext = "insert into orders values (" + customerid.tostring() + "," + itemid.tostring() + "," + itemqty.tostring() + " , getdate() ); select @@identity"; cmd.commandtext = insertsqltext; orderid = convert.toint32(cmd.executescalar()); cmd.dispose(); } catch (exception ex) { orderid = 0; throw (ex); } finally { con.close(); } return orderid; } private void page_load(object sender, system.eventargs e) { int orderid = purchaseitem(1, 1, 10); response.write(orderid); } #region web form designer generated code … }}提防到那些和ado.net工作的代码基础一律,咱们不过移除去对sqltransation东西的援用。那些工作被aspx页面包车型的士证明transaction="required"所包办。在工作统计中,当页面实行后,工作数量会减少,一个语句波折后,一切的语句将会回滚,你会看到工作被废除了。图7:asp.net工作的统计

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.