📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtOrderId;
private System.Windows.Forms.Button RollbackButton;
private System.Windows.Forms.Button CommitButton;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtOrderId = new System.Windows.Forms.TextBox();
this.RollbackButton = new System.Windows.Forms.Button();
this.CommitButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 0;
this.label1.Text = "OrderID";
//
// txtOrderId
//
this.txtOrderId.Location = new System.Drawing.Point(80, 40);
this.txtOrderId.Name = "txtOrderId";
this.txtOrderId.Size = new System.Drawing.Size(152, 21);
this.txtOrderId.TabIndex = 1;
this.txtOrderId.Text = "";
//
// RollbackButton
//
this.RollbackButton.Location = new System.Drawing.Point(32, 112);
this.RollbackButton.Name = "RollbackButton";
this.RollbackButton.Size = new System.Drawing.Size(88, 32);
this.RollbackButton.TabIndex = 2;
this.RollbackButton.Text = "Delete Order(Rollback)";
this.RollbackButton.Click += new System.EventHandler(this.RollbackButton_Click);
//
// CommitButton
//
this.CommitButton.Location = new System.Drawing.Point(160, 112);
this.CommitButton.Name = "CommitButton";
this.CommitButton.Size = new System.Drawing.Size(88, 32);
this.CommitButton.TabIndex = 3;
this.CommitButton.Text = "Delete Order(Commit)";
this.CommitButton.Click += new System.EventHandler(this.CommitButton_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.CommitButton,
this.RollbackButton,
this.txtOrderId,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void RollbackButton_Click(object sender, System.EventArgs e)
{
//Create a connection
SqlConnection cn = new SqlConnection(@"Data Source=(local); Integrated Security=SSPI;database=Northwind");
//Open the connection
cn.Open();
//Begin the transaction
SqlTransaction objTrans= cn.BeginTransaction();
try
{
//Create a command object
SqlCommand cmd= cn.CreateCommand();
//supply values to the command's properties
cmd.CommandType= CommandType.Text;
cmd.CommandText="DELETE Orders WHERE OrderId = " +
Convert.ToString(txtOrderId.Text);
//set the Transaction property of the command object
cmd.Transaction= objTrans;
//Delete the order
cmd.ExecuteNonQuery();
//Commit the Transaction
objTrans.Commit();
//If no exception has been raised, transaction has been committed - give message
MessageBox.Show ("Transaction Committed"+ "\n"+ "Order and Order Details have been successfully deleted", "Commit Transaction");
}
catch ( System.Data.SqlClient.SqlException e1)
{
//Rollback the transaction
objTrans.Rollback();
// Display Error Message.
MessageBox.Show ("Error-TRANSACTION ROLLED BACK " + "\n"+ e1.Message,"Rollback Transaction");
}
catch ( System.Exception e2)
{
// Display Error Message.
MessageBox.Show ("System Error " + "\n"+ e2.Message,"Error");
}
finally
{
//Close the connection
cn.Close();
}
}
private void CommitButton_Click(object sender, System.EventArgs e)
{
//Create a connection
SqlConnection cn = new SqlConnection(@"Data Source=(local); Integrated Security=SSPI;database=Northwind");
//Open the connection
cn.Open();
//Begin the transaction
SqlTransaction objTrans= cn.BeginTransaction();
try
{
//Create a command object
SqlCommand cmd = cn.CreateCommand();
//supply values to the command's properties
cmd.CommandType = CommandType.Text;
cmd.CommandText = "DELETE [Order Details] WHERE " +
"OrderId=" + Convert.ToString(txtOrderId.Text);
//set the Transaction property of the command object
cmd.Transaction= objTrans;
//Delete the order
cmd.ExecuteNonQuery();
cmd.CommandText="DELETE Orders WHERE " +
"OrderId=" + Convert.ToString(txtOrderId.Text);
//Delete the order
cmd.ExecuteNonQuery();
//Commit the Transaction
objTrans.Commit();
//If code comes here, transaction has been committed - give message
MessageBox.Show ("Transaction Committed"+ "\n"+ "Order and Order Details have been successfully deleted", "Commit Transaction");
}
catch ( System.Data.SqlClient.SqlException e1)
{
//Rollback the transaction
objTrans.Rollback();
// Display Error Message.
MessageBox.Show ("Error-TRANSACTION ROLLED BACK " + "\n"+ e1.Message,"Rollback Transaction");
}
catch ( System.Exception e2)
{
// Display Error Message.
MessageBox.Show ("System Error " + "\n"+ e2.Message,"Error");
}
finally
{
//Close the connection
cn.Close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -