阿牛 - 专注.NET开发

如果梦想与实现之间有一道不可逾越的鸿沟,那么“执行力”就是跨越这道鸿沟的桥梁。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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();
            
//.
posted on 2008-05-15 09:31  阿牛-专注金融行业开发  阅读(4244)  评论(0编辑  收藏  举报