【问题标题】:How to read a recordset multiple times?如何多次读取记录集?
【发布时间】:2013-11-05 17:39:32
【问题描述】:

我在 DataTable 中存储了一个 ADO 记录集。我已经迭代到它的结尾。在相同的代码中,我想重申它,但它给了我一个错误 -

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: There is no row at position -1.
   at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)
   at System.Data.RBTree`1.get_Item(Int32 index)
   at System.Data.DataRowCollection.get_Item(Int32 index)

我看过这里 - http://www.techrepublic.com/blog/10-things/10-plus-mistakes-to-avoid-when-using-vba-recordset-objects/ 但它并没有给我一个将“行指针”移回第一行的确切方法。

我该怎么做?

【问题讨论】:

  • 看起来你正在绑定.get_Item(-1)
  • 发布您的代码!我们不介意读者!
  • 不要使用 ADO。相反,请使用 DbConnection 和朋友,或者更好的是,使用实体框架。
  • 老式 ADO 曾经有一个 MoveFirst 方法。你能智能感知类似的东西吗?
  • @SLaks - 实际上,代码和完整的故事在这里 - stackoverflow.com/questions/19780858/looping-a-datatable-twice

标签: c#


【解决方案1】:

使用 Recordset.MoveFirst() 方法。但是,这仅在打开记录集以支持向后光标移动时才有效。

这是一个包含更多信息的链接: Here

【讨论】:

  • 我需要遍历存储在数据表中的记录集,而不是记录集本身。
  • 那有什么问题呢?只需对数据表的行进行 foreach,它不会跟踪游标,因此无需重置数据表。
猜你喜欢
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多