事务处理

来源:互联网 发布:安捷伦数据采集仪 编辑:程序博客网 时间:2024/05/24 07:35

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["Ccocn"].ConnectionString);
        Con.Open();
        SqlCommand Cmd = new SqlCommand();
        Cmd.Connection = Con;
        SqlTransaction ST = Con.BeginTransaction();
        Cmd.Transaction = ST;
        try
        {
            Cmd.CommandText = "Delete from Demo1 Where ID=4";
            Cmd.ExecuteNonQuery();

            Cmd.CommandText = "Delete from Dem2 Where ParentID=4";
            Cmd.ExecuteNonQuery();

            ST.Commit();
        }
        catch
        {
            ST.Rollback();
        }
        Con.Close();

    }

0 0