转换和处理 - IDataReader [重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

假设我创建了一个数据阅读器。

我在我的数据库类中有一个方法 - 以这种方式创建它:

db.ExecuteReaderIDb(sSQL)

我可以立即将它分配给using语句,它将在最后被处理掉,但我想使用HasRows属性,这意味着我想要转换为System.Data.Common.DbDataReader。

我的问题涉及对System.Data.Common.DbDataReader执行立即转换。

在这种情况下,如果我使用using语句,有两个问题:1。它是否创建了2个对象? 2.如果适用的话,如果我打电话处理我投放的内容,两个对象都会被处理掉吗?

否则,我是否需要2个using语句,返回IDataReader的语句,以及执行强制转换以确保所有内容都被正确清理的语句。

c# vb.net idisposable
1个回答
3
投票

转换不会创建新对象。

Using reader = db.ExecuteReaderIDb(sSQL)
  Dim someOtherReader = CType(reader, System.Data.Common.DbDataReader)

  ' someOtherReader and reader point to the same object
  ' disposing reader will also dispose someOtherReader
End Using

你总是可以在使用中投射

Using reader = CType(db.ExecuteReaderIDb(sSQL), System.Data.Common.DbDataReader)
    ...
End Using
© www.soinside.com 2019 - 2024. All rights reserved.