Tag: datareader

在C#中处理DBNull

有没有更好的/更干净的方法来做到这一点? int stockvalue = 0; if (!Convert.IsDBNull(reader["StockValue"])) stockvalue = (int)reader["StockValue"];

DataReader中的倍数表

我通常使用DataSet因为它非常灵活。 最近我被分配了代码优化任务,为了减less命中数据库,我正在改变一个程序中的两个查询。 一个查询返回count ,另一个返回actual data 。 也就是说,我的stored procedure返回两个表。 现在,我知道如何使用DataSets读取两个表,但是我需要使用DataReader读取两个表。 在寻找我发现这个 。 我按照这篇文章写这样的代码: dr = cmd.ExecuteReader(); while (dr.Read()) { } if (dr.NextResult()) // this line throws exception { while (dr.Read()) { 但是我在dt.NextResult得到一个exception。 例外是: Invalid attempt to call NextResult when reader is closed. 我也试图去Google上面的错误,但还是无法解决问题。 任何帮助都感激不尽。 我需要使用datareader读取多个表,这可能吗?

如何轻松地将DataReader转换为List <T>?

我有一个DataReader中的数据,我想要转换为List<T> 。 什么是可能的简单解决scheme呢? 例如在CustomerEntity类中,我有CustomerId和CustomerName属性。如果我的DataReader将这两列作为数据返回,那么如何将它转换为List<CustomerEntity> 。