> 데이터 베이스 > MySQL 튜토리얼 > 数据库事务使用范例

数据库事务使用范例

WBOY
풀어 주다: 2016-06-07 15:55:45
원래의
1108명이 탐색했습니다.

传入实体执行(可添加 修改 删除)事务。 IDbHelper dbHelper = new OracleHelper(ConfigHelper.GetConfigString( BusinessDbConnection )); bool result = true ; try { dbHelper.BeginTransaction(); //主表 TE_AREAManager manager = new TE_AREAManager(

传入实体执行(可添加 修改 删除)事务。

<code class="csharp plain">IDbHelper dbHelper = <code class="csharp keyword">new <code class="csharp plain">OracleHelper(ConfigHelper.GetConfigString(<code class="csharp string">"BusinessDbConnection"<code class="csharp plain">)); <code class="csharp spaces"> <code class="csharp keyword">bool <code class="csharp plain">result = <code class="csharp keyword">true<code class="csharp plain">; <code class="csharp spaces"> <code class="csharp keyword">try <code class="csharp spaces"> <code class="csharp plain">{ <code class="csharp spaces"> <code class="csharp plain">dbHelper.BeginTransaction(); <code class="csharp spaces"> <code class="csharp comments">//主表 <code class="csharp spaces"> <code class="csharp plain">TE_AREAManager manager = <code class="csharp keyword">new <code class="csharp plain">TE_AREAManager(dbHelper, userInfo); <code class="csharp spaces"> <code class="csharp plain">TE_AREAEntity tE_AREAEntity = manager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); <code class="csharp spaces"> <code class="csharp plain">manager.Delete(tE_AREAEntity); <code class="csharp comments">//子表 <code class="csharp spaces"> <code class="csharp plain">TE_AREA_SUBManager submanager = <code class="csharp keyword">new <code class="csharp plain">TE_AREA_SUBManager(dbHelper, userInfo); <code class="csharp spaces"> <code class="csharp plain">TE_AREA_SUBEntity tE_AREA_SUBEntity = submanager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); <code class="csharp spaces"> <code class="csharp plain">submanager.Delete(tE_AREA_SUBEntity); <code class="csharp comments">//事务提交  <code class="csharp spaces"> <code class="csharp plain">dbHelper.CommitTransaction(); <code class="csharp spaces"> <code class="csharp plain">} <code class="csharp spaces"> <code class="csharp keyword">catch<code class="csharp plain">(Exception ex) <code class="csharp spaces"> <code class="csharp plain">{ <code class="csharp comments">//事务回滚 <code class="csharp spaces"> <code class="csharp plain">dbHelper.RollbackTransaction(); <code class="csharp spaces"> <code class="csharp plain">result=<code class="csharp keyword">false<code class="csharp plain">; <code class="csharp spaces"> <code class="csharp plain">} <code class="csharp plain"> <code class="csharp plain"><strong>可以传入sql语句执行事务</strong> <code class="csharp plain"> <code class="csharp plain">IDbHelper dbHelper = <code class="csharp keyword">new <code class="csharp plain">OracleHelper(ConfigHelper.GetConfigString(<code class="csharp string">"BusinessDbConnection"<code class="csharp plain">)); <code class="csharp spaces"> <code class="csharp keyword">bool <code class="csharp plain">result = <code class="csharp keyword">true<code class="csharp plain">; <code class="csharp spaces"> <code class="csharp keyword">try <code class="csharp spaces"> <code class="csharp plain">{ <code class="csharp spaces"> <code class="csharp plain">dbHelper.BeginTransaction(); <code class="csharp spaces"> <code class="csharp keyword">string <code class="csharp plain">commandText = <code class="csharp string">" DELETE FROM TE_AREA WHERE ID=" <code class="csharp plain">+ dbHelper.SqlSafe(id); <code class="csharp spaces"> <code class="csharp plain">dbHelper.ExecuteNonQuery(sqlString); <code class="csharp spaces"> <code class="csharp plain">commandText = <code class="csharp string">" DELETE FROM TE_AREA_SUB WHERE ID=" <code class="csharp plain">+ dbHelper.SqlSafe(id); <code class="csharp spaces"> <code class="csharp plain">dbHelper.ExecuteNonQuery(commandText); <code class="csharp spaces"> <code class="csharp plain">dbHelper.CommitTransaction(); <code class="csharp spaces"> <code class="csharp plain">} <code class="csharp spaces"> <code class="csharp keyword">catch<code class="csharp plain">(Exception ex) <code class="csharp spaces"> <code class="csharp plain">{ <code class="csharp spaces"> <code class="csharp plain">dbHelper.RollbackTransaction(); <code class="csharp spaces"> <code class="csharp plain">result=<code class="csharp keyword">false<code class="csharp plain">; <code class="csharp spaces"> <code class="csharp plain">} <code class="csharp plain"><strong> 还可以同时传入实体,SQL语句</strong> <code class="csharp plain"> <code class="csharp plain">IDbHelper dbHelper = <code class="csharp keyword">new <code class="csharp plain">OracleHelper(ConfigHelper.GetConfigString(<code class="csharp string">"BusinessDbConnection"<code class="csharp plain">)); <code class="csharp spaces"> <code class="csharp keyword">bool <code class="csharp plain">result = <code class="csharp keyword">true<code class="csharp plain">; <code class="csharp spaces"> <code class="csharp keyword">try <code class="csharp spaces"> <code class="csharp plain">{ <code class="csharp spaces"> <code class="csharp plain">dbHelper.BeginTransaction(); <code class="csharp plain">              <code class="csharp comments">//主表 <code class="csharp spaces"> <code class="csharp plain">TE_AREAManager manager = <code class="csharp keyword">new <code class="csharp plain">TE_AREAManager(dbHelper, userInfo); <code class="csharp spaces"> <code class="csharp plain">TE_AREAEntity tE_AREAEntity = manager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); <code class="csharp spaces"> <code class="csharp plain">manager.Delete(tE_AREAEntity); <code class="csharp plain">              <code class="csharp comments">//子表 <code class="csharp spaces"> <code class="csharp plain">TE_AREA_SUBManager submanager = <code class="csharp keyword">new <code class="csharp plain">TE_AREA_SUBManager(dbHelper, userInfo); <code class="csharp spaces"> <code class="csharp plain">TE_AREA_SUBEntity tE_AREA_SUBEntity = submanager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID)); <code class="csharp spaces"> <code class="csharp plain">submanager.Delete(tE_AREA_SUBEntity); <code class="csharp plain">               <code class="csharp plain">              <code class="csharp comments">//执行SQL语句 <code class="csharp plain">              <code class="csharp keyword">string <code class="csharp plain">commandText = <code class="csharp string">" DELETE FROM JINTIANDA WHERE ID=" <code class="csharp plain">+ dbHelper.SqlSafe(id); <code class="csharp plain">              dbHelper.ExecuteNonQuery(commandText); <code class="csharp plain">              <code class="csharp comments">//事务提交  <code class="csharp spaces"> <code class="csharp plain">dbHelper.CommitTransaction(); <code class="csharp spaces"> <code class="csharp plain">} <code class="csharp spaces"> <code class="csharp keyword">catch<code class="csharp plain">(Exception ex) <code class="csharp spaces"> <code class="csharp plain">{ <code class="csharp plain">             <code class="csharp comments">//事务回滚 <code class="csharp spaces"> <code class="csharp plain">dbHelper.RollbackTransaction(); <code class="csharp spaces"> <code class="csharp plain">result=<code class="csharp keyword">false<code class="csharp plain">; <code class="csharp spaces"> <code class="csharp plain">}
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿