asp.net 使用事務(wù)
更新時(shí)間:2010年02月25日 16:53:22 作者:
假如我們要同時(shí)更新幾張表就必定需要使用事務(wù)操作。
ASP.NET操作事務(wù)非常的簡單,代碼如下:
代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
public partial class swstudy : System.Web.UI.Page
{
private SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
}
private SqlConnection GetConnection()
{
if (conn!=null&&conn.State == ConnectionState.Open)
{
return conn;
}
else
{
return conn= new SqlConnection("server=PC-200911131211;database=test;uid=sa;pwd=sa123456");
}
}
//提交
protected void Button1_Click(object sender, EventArgs e)
{
string str1 = this.TextBox1.Text;
string str2 = this.TextBox2.Text;
using (GetConnection())
{
conn.Open();
//事務(wù)
SqlTransaction tran;
tran = conn.BeginTransaction();
//提交一
SqlCommand cmd1 = new SqlCommand("insert into m1(m1,m2)values('" + str1 + "','" + str2 + "')", conn);
cmd1.Transaction = tran;
//提交二
SqlCommand cmd2 = new SqlCommand("insert into messages(msg)values('" + str1 + "')", conn);
cmd2.Transaction = tran;
try
{
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
tran.Commit();//提交事物
Response.Write("<script>alert('全部提交OK')</script>");
}
catch
{
tran.Rollback();//回滾操作
Response.Write("<script>alert('事務(wù)回滾')</script>");
}
finally
{
conn.Close();
}
}
}
}
代碼
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
public partial class swstudy : System.Web.UI.Page
{
private SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
}
private SqlConnection GetConnection()
{
if (conn!=null&&conn.State == ConnectionState.Open)
{
return conn;
}
else
{
return conn= new SqlConnection("server=PC-200911131211;database=test;uid=sa;pwd=sa123456");
}
}
//提交
protected void Button1_Click(object sender, EventArgs e)
{
string str1 = this.TextBox1.Text;
string str2 = this.TextBox2.Text;
using (GetConnection())
{
conn.Open();
//事務(wù)
SqlTransaction tran;
tran = conn.BeginTransaction();
//提交一
SqlCommand cmd1 = new SqlCommand("insert into m1(m1,m2)values('" + str1 + "','" + str2 + "')", conn);
cmd1.Transaction = tran;
//提交二
SqlCommand cmd2 = new SqlCommand("insert into messages(msg)values('" + str1 + "')", conn);
cmd2.Transaction = tran;
try
{
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
tran.Commit();//提交事物
Response.Write("<script>alert('全部提交OK')</script>");
}
catch
{
tran.Rollback();//回滾操作
Response.Write("<script>alert('事務(wù)回滾')</script>");
}
finally
{
conn.Close();
}
}
}
}
您可能感興趣的文章:
- 如何在數(shù)據(jù)庫中用好Transaction?
- asp.net 執(zhí)行事務(wù)代碼
- c# 執(zhí)行事務(wù)函數(shù)代碼
- Asp.net 在三層架構(gòu)中事務(wù)的使用實(shí)例代碼
- c#實(shí)現(xiàn)sqlserver事務(wù)處理示例
- C#中TransactionScope的使用小結(jié)
- C#事務(wù)處理(Execute Transaction)實(shí)例解析
- C#執(zhí)行SQL事務(wù)用法實(shí)例
- 在ASP.NET 2.0中操作數(shù)據(jù)之六十一:在事務(wù)里對(duì)數(shù)據(jù)庫修改進(jìn)行封裝
相關(guān)文章
ASP.NET MVC如何使用Unity實(shí)現(xiàn)Ioc詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET MVC如何使用Unity實(shí)現(xiàn)Ioc的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Asp.Net?Core7?preview4限流中間件新特性詳解
這篇文章主要為大家介紹了Asp.Net?Core7?preview4限流中間件的新特性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05asp.net實(shí)現(xiàn)根據(jù)城市獲取天氣預(yù)報(bào)的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)根據(jù)城市獲取天氣預(yù)報(bào)的方法,涉及asp.net調(diào)用新浪接口獲取天氣預(yù)報(bào)信息的實(shí)現(xiàn)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-12-12ASP.NETCore6開啟文件服務(wù)允許通過url訪問附件的操作方法
最近在做一個(gè)工作臺(tái)的文件上傳下載功能,主要想實(shí)現(xiàn)上傳圖片之后,可以通過url直接訪問,由于url直接訪問文件不安全,所以需要手動(dòng)開啟文件服務(wù),這篇文章主要介紹了ASP.NETCore6開啟文件服務(wù)允許通過url訪問附件,需要的朋友可以參考下2023-11-11把a(bǔ)spx頁面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼
把a(bǔ)spx頁面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼,主要是利于搜索引擎的收錄。2011-10-10Path類與Directory類與File類對(duì)路徑/目錄/文件的操作實(shí)例
本文將詳細(xì)介紹下:Path對(duì)路徑字符串進(jìn)行操作/Directory和DirectoryInfo 對(duì)目錄進(jìn)行操作/File和FileInfo對(duì)文件進(jìn)行操作,感興趣的你可不要錯(cuò)過了哈2013-02-02