C# sql数据库事务操作

        string str = "";
        SqlConnection conn1 = null;
        SqlTransaction tran1 = null;
        try
        {
            conn1 = new SqlConnection(SqlHelper.cnn.ConnectionString);
            tran1 =conn1.BeginTransaction();  //事务开始
            conn1.Open();
            SqlCommand comm1 = conn1.CreateCommand();
            comm1.Connection = conn1;
            comm1.Transaction = tran1;
            foreach (DataStruct.MaCompenstateLevel TmpLevel in MaCompenstateLevel)
            {
                str = string.Format("update b_compenstateList set xx=yy”);
                comm1.CommandText = str;
                comm1.ExecuteNonQuery();

            }
            tran1.Commit();   //完成提交
            return (int)OperateResultEnum.successful;
        }
        catch
        {
            if (tran1 != null)
            {
                try
                {
                    tran1.Rollback();  //失败回滚
                    return (int)OperateResultEnum.failure;
                }
                catch
                {
                    return (int)OperateResultEnum.exception;
                }
            }
        }
        finally
        {
            conn1.Close();
        }

你可能感兴趣的:(sql)