发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
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
}
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。