zoukankan      html  css  js  c++  java
  • 解决ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.

    ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.

    同一个connection,在update的command上启动了一个transaction,后面一个查询的command,没有设置transaction属性,所以出了这个exception.

    解决方法:

    将update的command加到select的command上去.

     

    SqlConnection conn = new SqlConnection("");
                SqlCommand updateCmd 
    = new SqlCommand("update", conn);
                updateCmd.Transaction 
    = conn.BeginTransaction();
                
    int rows = updateCmd.ExecuteNonQuery();

                SqlCommand selectCmd 
    = new SqlCommand("select ", conn);
                selectCmd.Transaction 
    = updateCmd.Transaction;//important

                SqlDataReader reader 
    = selectCmd.ExecuteReader();
                
    //.
  • 相关阅读:
    bzoj4196: [Noi2015]软件包管理器
    bzoj3083: 遥远的国度
    bzoj4034: [HAOI2015]T2
    2.EXIT_KEY
    AD如何1比1打印
    编程时注意,
    同步事件、异步事件、轮询
    事件位
    挂起进程相关API
    PROCESS_EVENT_POLL事件
  • 原文地址:https://www.cnblogs.com/rockniu/p/1197672.html
Copyright © 2011-2022 走看看