黄聪

论SEO对人类的重要性,请看我的博客:hcsem.com

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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.OleDb;
using System.Web;
using System.Data.SqlClient;

namespace 数据回滚
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "/data.mdb");

        public void NonReturnDataCommand(params string[] values)
        {
            OleDbCommand cmd = new OleDbCommand("", conn);
            conn.Open();
            OleDbTransaction tran = conn.BeginTransaction();
            cmd.Transaction = tran;

            try
            {
               
                foreach (string sql in values)
                {
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
               
                tran.Commit();
            }
            catch(Exception e)
            {
                tran.Rollback();
                MessageBox.Show(e.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sqlTran = "insert into B(B1,B2) values(1,555)";
            string sqlNoTran = "insert into A(A1,A2) values(2,666)";

            NonReturnDataCommand( sqlNoTran,sqlTran);
        }
    }
}

posted on 2010-03-26 15:48  黄聪  阅读(1110)  评论(0编辑  收藏  举报