C#进行SQL server数据库操作时,事务中能不能改变查询语句和参数等,会不会使事务失效?


像是这样:

public static bool DeletePlanByPlanUid(List
<string>
 planUidList)
{
    SqlConnection conn = new SqlConnection(DataBase.connectString);
    conn.Open();
    SqlTransaction transaction = conn.BeginTransaction();
    SqlCommand cmd = conn.CreateCommand();
    cmd.Transaction = transaction;
    try
    {
        foreach (string planUid in planUidList)
        {//liuhong
            string sqlStr = SqlStr.setPlanDeleted;
            cmd.CommandText = sqlStr;
            cmd.Parameters.AddWithValue("@PlanUid", planUid);
            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
        }

        transaction.Commit();
        return true;
    }
    catch (Exception ex)
    {
        transaction.Rollback();
        return false;
    }
    finally
    {
        conn.Close();
    }
}
</string>

数据库 c#

十六夜。咲夜 10 years, 3 months ago

Your Answer