CSharp Open Connection And Begin Transaction – ADO.Net Database

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">SqlTransaction _Transact;void page_load(Object Sender, EventArgs E)
  String ConnectionString = "Server=localhost; Database=pubs; uid=sa; pwd=;";
  String CommandText = "UPDATE authors SET state='PU' WHERE state='UT'";
  String CommandText2 = "UPDATE authors SET au_id='1' WHERE au_id='2'";
    SqlConnection _Connection = new SqlConnection(ConnectionString);
    SqlCommand _Command = new SqlCommand();
        _Transact = _Connection.BeginTransaction();
        _Command.Connection = _Connection;
        _Command.Transaction = _Transact;
        _Command.CommandText = CommandText;
        _Command.CommandText = CommandText2;
        MessageLiteralControl.Text = "Transaction Successful!";
    }catch (Exception Err){
        MessageLiteralControl.Text = "Transaction Failed!<br/>" + Err.Message;
    <form runat="server" ID="Form1">
        <asp:Label id="MessageLabel" runat="server" EnableViewState="false"></asp:Label>

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.