C#事务处理简明示例

来源:互联网 发布:天地知我心二我与空姐 编辑:程序博客网 时间:2024/05/21 21:37

定义数据库:

create database guigui
use guigui

create table users
(
 userid int not null primary key identity(1,1),
 username varchar(20),
 userpass varchar(20)
)

create table caibao
(
 cid int not null primary key,
 cname varchar(20) not null
)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace checkedListBoxTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(73, 97);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 2;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(28, 136);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 3;
            this.button1.Text = "确 定";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(272, 97);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 21);
            this.textBox2.TabIndex = 4;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 100);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 12);
            this.label1.TabIndex = 5;
            this.label1.Text = "财宝编号";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(192, 100);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 6;
            this.label2.Text = "财宝名";
            //
            // textBox3
            //
            this.textBox3.Location = new System.Drawing.Point(73, 36);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 21);
            this.textBox3.TabIndex = 7;
            //
            // textBox4
            //
            this.textBox4.Location = new System.Drawing.Point(272, 36);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(100, 21);
            this.textBox4.TabIndex = 8;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(12, 39);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 9;
            this.label3.Text = "用户名";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(192, 39);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(41, 12);
            this.label4.TabIndex = 10;
            this.label4.Text = "密  码";
            //
            // Form2
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 273);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection dbconn = new SqlConnection("server=.;user id =sa;password=123456;database=guigui"))
            {
                dbconn.Open();

              SqlTransaction trans =   dbconn.BeginTransaction();

                SqlCommand cmd = dbconn.CreateCommand();
                cmd.Transaction=trans;
                cmd.Connection = dbconn;
                try
                {
                    cmd.CommandText = "insert into users (username,userpass) values('" + textBox3.Text + "','" + textBox4.Text + "')";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("主表数据插入成功");
                    cmd.CommandText = "insert into caibao (cid,cname) values(" + textBox1.Text + ",'" + textBox2.Text + "')";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("子表数据插入成功");
                    trans.Commit();
                    MessageBox.Show("事务提交");
                }
                catch
                {
                    trans.Rollback();
                    MessageBox.Show("事务回滚");
                }


                cmd.Dispose();
                trans.Dispose();
                dbconn.Dispose();
            }
        }
    }
}