小格
没有目标的人,是在帮那些有目标的人完成目标。

  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();//出现错误,进行回滚
                    }
                }
            }

posted on 2013-12-19 18:21  小格  阅读(160)  评论(0编辑  收藏  举报