C#執(zhí)行SQL事務(wù)用法實例
更新時間:2015年01月21日 14:14:57 投稿:shichen2014
這篇文章主要介紹了C#執(zhí)行SQL事務(wù)用法,實例分析了通過C#中提供的Transaction執(zhí)行SQL事務(wù)的使用技巧,需要的朋友可以參考下
本文實例講述了C#執(zhí)行SQL事務(wù)用法。分享給大家供大家參考。具體分析如下:
1.通過存儲過程。
2.通過C#中提供的Transaction。這里就來演示一下通過C#中提供的Transaction 執(zhí)行SQL事務(wù)。
WebForm3.aspx.cs頁面
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
namespace 用戶激活
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connStr=ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn=new SqlConnection(connStr))
{
conn.Open();
using (SqlTransaction tran = conn.BeginTransaction()) //開始數(shù)據(jù)庫事務(wù)。即創(chuàng)建一個事務(wù)對象tran
{
using (SqlCommand cmd =new SqlCommand())
{
cmd.Connection = conn;
cmd.Transaction = tran; //獲取或設(shè)置將要其執(zhí)行的事務(wù)
try
{
//在try{}塊里執(zhí)行sqlconnection命令
cmd.CommandText = "update bb set Moneys=Moneys-" + Money.Text + " where ID=" + ToID.Text;
cmd.ExecuteNonQuery();
cmd.CommandText = "update bb set Moneys=Moneys+" + Money.Text + " where ID=" + FromID.Text;
cmd.ExecuteNonQuery();
tran.Commit(); //如果兩條sql命令都執(zhí)行成功,則執(zhí)行commit這個方法來執(zhí)行這些操作。
Msg.Text = "轉(zhuǎn)賬成功";
}
catch
{
Msg.Text = "轉(zhuǎn)賬失敗";
tran.Rollback();//如果執(zhí)行不成功,發(fā)送異常,則執(zhí)行rollback方法,回滾到事務(wù)操作開始之前。
}
}
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
namespace 用戶激活
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connStr=ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn=new SqlConnection(connStr))
{
conn.Open();
using (SqlTransaction tran = conn.BeginTransaction()) //開始數(shù)據(jù)庫事務(wù)。即創(chuàng)建一個事務(wù)對象tran
{
using (SqlCommand cmd =new SqlCommand())
{
cmd.Connection = conn;
cmd.Transaction = tran; //獲取或設(shè)置將要其執(zhí)行的事務(wù)
try
{
//在try{}塊里執(zhí)行sqlconnection命令
cmd.CommandText = "update bb set Moneys=Moneys-" + Money.Text + " where ID=" + ToID.Text;
cmd.ExecuteNonQuery();
cmd.CommandText = "update bb set Moneys=Moneys+" + Money.Text + " where ID=" + FromID.Text;
cmd.ExecuteNonQuery();
tran.Commit(); //如果兩條sql命令都執(zhí)行成功,則執(zhí)行commit這個方法來執(zhí)行這些操作。
Msg.Text = "轉(zhuǎn)賬成功";
}
catch
{
Msg.Text = "轉(zhuǎn)賬失敗";
tran.Rollback();//如果執(zhí)行不成功,發(fā)送異常,則執(zhí)行rollback方法,回滾到事務(wù)操作開始之前。
}
}
}
}
}
}
}
WebForm3.aspx頁面
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="用戶激活.WebForm3" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
轉(zhuǎn)賬賬戶:<asp:TextBox ID="ToID" runat="server"></asp:TextBox></br>
接收賬戶:<asp:TextBox ID="FromID" runat="server"></asp:TextBox></br>
轉(zhuǎn)賬金額:<asp:TextBox ID="Money" runat="server"></asp:TextBox></br>
轉(zhuǎn)賬是否成功:<asp:Label ID="Msg" runat="server" Text=""></asp:Label></br>
<asp:Button ID="Button1" runat="server" Text="提交轉(zhuǎn)賬" onclick="Button1_Click" />
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
轉(zhuǎn)賬賬戶:<asp:TextBox ID="ToID" runat="server"></asp:TextBox></br>
接收賬戶:<asp:TextBox ID="FromID" runat="server"></asp:TextBox></br>
轉(zhuǎn)賬金額:<asp:TextBox ID="Money" runat="server"></asp:TextBox></br>
轉(zhuǎn)賬是否成功:<asp:Label ID="Msg" runat="server" Text=""></asp:Label></br>
<asp:Button ID="Button1" runat="server" Text="提交轉(zhuǎn)賬" onclick="Button1_Click" />
</form>
</body>
</html>
運行結(jié)果如下圖所示:
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
淺談c#.net中巧用ToString()將日期轉(zhuǎn)成想要的格式
有時候我們要對時間進(jìn)行轉(zhuǎn)換,達(dá)到不同的顯示效果,更多的該怎么辦呢?2013-03-03