您的位置:首页 > 产品设计 > UI/UE

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

2008-05-15 09:31 796 查看
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();

//.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐