ExecuteReader requires command to have transaction when connection assigned to command is in pending local trans

前端 未结 2 362
遥遥无期
遥遥无期 2021-01-17 08:46

i have to insert in two tables with single transaction, query which have to implement are below. secondly getting exception at SqlDataReader read = comm.ExecuteR

相关标签:
2条回答
  • 2021-01-17 09:21

    You might want to consider switching to using TransactionScope which is then used implicitly for all commands within it. You'd use it something like:

    using(var scope = new TransactionScope())
    {
       using(var conn = new SqlConnection(/*...*/))
       {
          //As many nested commands, etc, using the above connection.
          //but don't need to create a SqlTransaction object nor
          //in any way reference the scope variable
       }
       scope.Complete();
    }
    
    0 讨论(0)
  • 2021-01-17 09:35

    problem is you execute cmd1 on the same connection as cmd so there is an open transaction on that connection but you don't set cmd1.Transaction ... so solution would be to

    cmd1.Transaction = transaction;
    

    before

    cmd1.ExecuteNonQuery();
    
    0 讨论(0)
提交回复
热议问题