how to use sqltransaction in c#

前端 未结 6 2119
执笔经年
执笔经年 2020-12-05 10:56

I am using following code to execute two commands at once. I used sqltransaction to assure either all command get executed or rolled back.When I run my program without \"tra

6条回答
  •  攒了一身酷
    2020-12-05 11:28

    You have to tell your SQLCommand objects to use the transaction:

    cmd1.Transaction = transaction;
    

    or in the constructor:

    SqlCommand cmd1 = new SqlCommand("select...", connectionsql, transaction);
    

    Make sure to have the connectionsql object open, too.

    But all you are doing are SELECT statements. Transactions would benefit more when you use INSERT, UPDATE, etc type actions.

提交回复
热议问题