using语句下使用SqlDataReader异常:Invalid attempt to Read when reader is closed
2017-11-08 00:55
656 查看
学习.NET已经一周多了,这几天学习了一下MVC分层开发,就想封装一个获取SqlDataReader的工具类。但是在实际操作过程中却发生了异常。首先了解一下using语句,如果用using{}将代码块包起来,那么using中定义的连接信息,最后不用手动进行Close,会自动关闭,释放资源。
封装的工具类中的代码如下:
工具类在使用时,调用GetDataReader方法报错:“Invalid attempt to Read when reader is closed”。大概意思就是当reader关闭的时候,无法进行阅读。
查阅了一些资料以后,了解了原因:
using{}中代码执行完之后,连接自动关闭,而Reader与DataSet不同,DataSet已经将数据保存在本地内存中,而Reader并没有将数据保存在本地内存中,可以理解成只是指向了数据,连接关闭后用reader读取数据当然无法实现。
于是为了避免产生这种情况,我改用DataSet进行了工具类的封装,详细请见另一篇博客:
http://blog.csdn.net/datoubangzhu/article/details/78474138
封装的工具类中的代码如下:
public static SqlDataReader GetDataReader(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { SqlCommand cmd = new SqlCommand(sql, conn); foreach (SqlParameter parameter in parameters) { cmd.Parameters.Add(parameter); } try { conn.Open(); SqlDataReader sdr = cmd.ExecuteReader(); return sdr; } catch (Exception e) { throw; } } }
工具类在使用时,调用GetDataReader方法报错:“Invalid attempt to Read when reader is closed”。大概意思就是当reader关闭的时候,无法进行阅读。
查阅了一些资料以后,了解了原因:
using{}中代码执行完之后,连接自动关闭,而Reader与DataSet不同,DataSet已经将数据保存在本地内存中,而Reader并没有将数据保存在本地内存中,可以理解成只是指向了数据,连接关闭后用reader读取数据当然无法实现。
于是为了避免产生这种情况,我改用DataSet进行了工具类的封装,详细请见另一篇博客:
http://blog.csdn.net/datoubangzhu/article/details/78474138
相关文章推荐
- Invalid attempt to read when no data is present.
- Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not
- 使用OleDb写入Excel异常,"字段太小而不能接受所要添加的数据的数量" "the field is too small to accept the amount of data "
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- 执行update操作的话,就会报“Connection is read-only. Queries leading to data modification are not allowed”的异常。
- 在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be closed first”
- System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Food' when IDENTITY_INSERT is set to OFF
- c#数据库访问返回值类型为SqlDataReader时使用using时注意的问题
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- ### Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are
- Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- How to correctly read data when using epoll_wait
- 在使用Linq to SQL并序列化一个数据对象的时候报System.InvalidOperationException异常,序列化类型XXX的对象时检测到循环引用。
- 异常—Cause: java.sql.SQLException: Data source is closed