zoukankan      html  css  js  c++  java
  • 事务

      using (SqlConnection conn = new SqlConnection("server=192.168.120.209;database=test;uid=sa;pwd=Djhu2013"))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        SqlTransaction transaction = conn.BeginTransaction();//开始事物
                        cmd.Transaction = transaction;
                        try
                        {
                            cmd.Connection = conn;
                            cmd.CommandText = "insert into test1 values ('lily')select @@IDENTITY";
                            int paintID = Convert.ToInt32(cmd.ExecuteScalar());

                            cmd.CommandText = "insert into test2 values(" + paintID + ",'lily')";
                            cmd.ExecuteNonQuery();

                            transaction.Commit();//如果都成功那么提交事物
                        }
                        catch (Exception ex)
                        {
                            Console.Write(ex.Message);
                            transaction.Rollback();//出现错误,进行回滚
                        }
                    }
                }

  • 相关阅读:
    冒泡排序
    希尔排序
    现实中遇到的业务状况记录
    OneZero第二周第二次站立会议(2016.3.29)
    OneZero第二周第一次站立会议(2016.3.28)
    OneZero第五次站立会议(2016.3.25)
    OneZero第四次站立会议(2016.3.24)
    PSP(3.16——3.22)以及周记录
    OneZero第三次站立会议(2016.3.23)
    读“软工实践总结作业”有感
  • 原文地址:https://www.cnblogs.com/xiaogelove/p/3482685.html
Copyright © 2011-2022 走看看