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上去.

 

解决ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.SqlConnection conn = new SqlConnection("解决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.            SqlCommand updateCmd 
= new SqlCommand("update解决ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.", conn);
解决ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.            updateCmd.Transaction 
= conn.BeginTransaction();
解决ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.            
int rows = updateCmd.ExecuteNonQuery();
解决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.            SqlCommand selectCmd 
= new SqlCommand("select 解决ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.", conn);
解决ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.            selectCmd.Transaction 
= updateCmd.Transaction;//important
解决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.            SqlDataReader reader 
= selectCmd.ExecuteReader();
解决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..

相关文章: