C#事務

來源:互聯網
上載者:User

標籤:

<1>

C# 執行SQL事務 又兩種方法(本人總結的)

1.通過預存程序。

2.通過C#中提供的 Transaction 。 現在就就示範一下通過C#中提供的Transaction 執行SQL事務。


WebForm3.aspx.cs頁面

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()) //開始資料庫事務。即建立一個事務對象tran                {                    using (SqlCommand cmd =new SqlCommand())                    {                        cmd.Connection = conn;                        cmd.Transaction = tran; //擷取或設定將要其執行的事務                        try                        {                            //在try{}塊裡執行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命令都執行成功,則執行commit這個方法來執行這些操作。                            Msg.Text = "轉賬成功";                        }                        catch                        {                            Msg.Text = "轉賬失敗";                            tran.Rollback();//如果執行不成功,發送異常,則執行rollback方法,復原到事務操作開始之前。                        }                                            }                }            }        }    }}

<>

WebForm3.aspx頁面

<%@ 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">    轉賬賬戶:<asp:TextBox ID="ToID" runat="server"></asp:TextBox></br>    接收賬戶:<asp:TextBox ID="FromID" runat="server"></asp:TextBox></br>    轉賬金額:<asp:TextBox ID="Money" runat="server"></asp:TextBox></br>    轉賬是否成功:<asp:Label ID="Msg" runat="server" Text=""></asp:Label></br>    <asp:Button ID="Button1" runat="server" Text="提交轉賬" onclick="Button1_Click" />    </form></body></html>


C#事務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.