RSS订阅优然探索
你的位置:首页 » 学习收藏 » 正文

C#技术学习之-SqlTransaction

选择字号: 超大 标准 发布时间:2008-9-28 9:25:42 | 作者:admin | 0个评论 | 人浏览

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace Mirror.AutoWeb.HelpCSharp
{
 /// <summary>
 /// StudyTransaction 的摘要说明。
 /// </summary>
 public class StudyTransaction : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Literal Literal1;
  protected System.Web.UI.WebControls.Literal Literal2;
  protected System.Web.UI.WebControls.Literal Literal3;
  protected System.Web.UI.WebControls.Literal Literal4;
  private readonly string connstr = ConfigurationSettings.AppSettings["MySQLConnString1"].ToString();
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
    this.Literal2.Text=FormatDataReader(ExecuteReader());
    ExecuteTransation("10932 Bigge Rd.10932 Bigge Rd.","309 63rd St. #411309 63rd St. #411");
    this.Literal4.Text=FormatDataReader(ExecuteReader());

   }
  }
  private void ExecuteTransation(string newName,string newOtherName)
  {
   string cmdText ="update authors Set address='" + newName.ToString() + "' Where au_id='172-32-1176'";
   SqlConnection myConn = new SqlConnection(connstr);
   SqlCommand myCmd = new SqlCommand(cmdText,myConn);
   myConn.Open();
   SqlTransaction myTransaction = myConn.BeginTransaction();
   myCmd.Transaction = myTransaction;
   try
   {
    myCmd.ExecuteNonQuery();
    cmdText ="update authors Set address='" + newOtherName.ToString() + "' Where au_id='213-46-8915'";
    myCmd.CommandText=cmdText;
    myCmd.ExecuteNonQuery();
    myTransaction.Commit();
    htm = htm&("修改两条记录成功!");
   }
   catch(Exception ex)
   {
    htm = htm&(ex.Message.ToString());
   }
   finally
   {
    myConn.Close();
   }
  }
  private string FormatDataReader(SqlDataReader datareader)
  {
   string readerString = "";
   while(datareader.Read())
   {
    readerString += "au_id=" + datareader["au_id"].ToString() + "<br>";
    readerString += "au_lname=" + datareader["au_lname"].ToString() + "<br>";
    readerString += "address=" + datareader["address"].ToString() + "";
    readerString += "<br>";
   }
   datareader.Close();
   return readerString;
  }
  private SqlDataReader ExecuteReader()
  {
   string cmdText= " Select * From authors";
   SqlConnection myconn = new SqlConnection(connstr);
   SqlCommand mycmd = new SqlCommand(cmdText,myconn);
   myconn.Open();
   SqlDataReader dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
   return dr;
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}
 

标签:SqlTransaction  C#  

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。