ADO.NET记录导航

发布于 2024-07-08 17:41:08 字数 341 浏览 12 评论 0原文

我已经在 VB6 和 VB.NET 中进行了开发,并且在 VB6 中使用了 ADODB 对象来处理记录集导航(即 MoveFirst、MoveNext 等方法),并且使用 ADO.NET 来处理 ADO.NET 中的查询。逐行性质(即For Each Row In Table.Rows ...)

但现在我似乎陷入了困境。 我现在正在 VB.NET 中构建一个程序,我需要使用旧 Recordset 对象的 Move 命令的等效功能。 VB.NET 是否有某种支持此功能的对象,或者我是否必须求助于使用旧的 ADODB COM 对象?

编辑:为了澄清起见,我希望用户能够向前或向后导航查询。 循环遍历行是一项简单的任务。

I've done development in both VB6 and VB.NET, and I've used ADODB objects in VB6 to handle recordset navigation (i.e. the MoveFirst, MoveNext, etc. methods), and I have used ADO.NET to handle queries in a row-by-row nature (i.e For Each Row In Table.Rows ...)

But now I seem to have come to a dilemma. I am now building a program in VB.NET where I need to use the equivalent functionality of the Move commands of the old Recordset object. Does VB.NET have some sort of object that supports this functionality, or do I have to resort to using the old ADODB COM object?

Edit: Just for clarification, I want the user to be able to navigate through the query moving forwards or backwards. Looping through the rows is a simple task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听不够的曲调 2024-07-15 17:41:08

没有必要回到过去糟糕的日子。 如果你能给出一个伪代码示例,我可以帮你翻译成vb.net。

这是一种通用的方法。

Dim ds as DataSet

'populate your DataSet'

For each dr as DataRow in ds.Tables(<tableIndex>).Rows
  'Do something with the row'

Next

每次编辑 1:用户将导航结果,而不是查询。 ,您想要做的是 a) 获取结果并仅向它们显示 ds.Tables.Row() 的当前 rowindex,或者 b) 在每次导航时执行新查询(这不是一个真正性能良好的选项。)

因此 评论:不,他们没有。 但用户通常不会像这样与数据库交互工作。 您将需要获取结果的数据集/表,并使用按钮从数据集/表中检索相关行。

  • 第一行是 DataTable.Rows(0)
  • 最后一行是 DataTable.Rows(DataTable.Rows.Count-1)
    • 对于中间的任何行(将当前显示的行索引存储在应用中),然后调用
  • DataTable.Rows(currentRowIndex -1) 获取上一个行,调用
  • DataTable.Rows(currentRowIndex +1) 获取下一个行。

There is no need to go back to the bad old days. If you can give a pseudo code example, I can translate to vb.net for you.

This is kind of a generic way to do it.

Dim ds as DataSet

'populate your DataSet'

For each dr as DataRow in ds.Tables(<tableIndex>).Rows
  'Do something with the row'

Next

Per Edit 1: The user will navigate the results, not the query. So what you want to do is either a) get the results and display only the current rowindex of ds.Tables.Row() to them, or b) execute a new query with each navigation (not a real well performing option.)

Per comment: No, they havent. But the user usually will not be working interactively with the database like this. You will need to get your dataset / table of the results, and use the buttons to retrieve the relevant row from the dataset/table.

  • The First Row is DataTable.Rows(0)
  • The Last Row is DataTable.Rows(DataTable.Rows.Count-1)
    • for any row in between (store the currently displayed rowindex in your app), then call
  • DataTable.Rows(currentRowIndex -1) for previous and
  • DataTable.Rows(currentRowIndex +1) for next.
旧梦荧光笔 2024-07-15 17:41:08

这一切都取决于用途:
如果您只需要列出一个或多个查询的结果,则应该使用数据读取器。 DOK 是否指出过,它是只读且只能前进的,因此速度很快。
http://www.startvbdotnet.com/ado/sqlserver.aspx

如果您需要要导航记录,您应该使用数据集。
http://www.c-sharpcorner.com/ UploadFile/raghavnayak/DataSetsIn.NET12032005003647AM/DataSetsIn.NET.aspx

该数据集还具有“断开连接”工作的优点,因此您可以构建所有逻辑,并且只有在需要数据时才调用 Fill 方法。 数据集已填充,然后您可以开始使用数据,现在与数据库断开连接。

希望能帮助到你,
布鲁诺·菲格雷多
http://www.brunofigueiredo.com

It all depends on the usage:
If you need only to list the results of one or more queries you should use the datareader. Has DOK pointed out, it's read-only and foward-only so it's fast.
http://www.startvbdotnet.com/ado/sqlserver.aspx

If you need to navigate thou the records you should use a dataset.
http://www.c-sharpcorner.com/UploadFile/raghavnayak/DataSetsIn.NET12032005003647AM/DataSetsIn.NET.aspx

The dataset also has the advantage of working "disconnected", so you build all the logic, and only when you need the data you call the Fill method. The dataset is populated and then you can start working with the data, now disconnected from the DB.

Hope it helps,
Bruno Figueiredo
http://www.brunofigueiredo.com

超可爱的懒熊 2024-07-15 17:41:08

这是使用数据读取器的一个简单示例:

            Dim cmd As New OleDb.OleDbCommand(sql, Conn) 'You can also use command parameter here
            Dim dr As OleDb.OleDbDataReader
            dr = cmd.ExecuteReader

            While dr.Read

          ‘Do something with data
          ‘ access fields
          dr("fieldname")
          ‘Check for null
          IsDBNull(dr("fieldname"))

            End While

            dr.Close()

Here's a quick example of using the datareader:

            Dim cmd As New OleDb.OleDbCommand(sql, Conn) 'You can also use command parameter here
            Dim dr As OleDb.OleDbDataReader
            dr = cmd.ExecuteReader

            While dr.Read

          ‘Do something with data
          ‘ access fields
          dr("fieldname")
          ‘Check for null
          IsDBNull(dr("fieldname"))

            End While

            dr.Close()
掌心的温暖 2024-07-15 17:41:08

在.Net 中,有很多方法可以做到这一点。 我喜欢的一种方法是使用 DataReader,它可以返回多个记录集。 您可以使用 While DataReader.Read 循环遍历其记录。

使用 DataReader 的优点之一是它是一个只进、只读对象,因此速度快且重量轻。

为了允许用户一次浏览所有记录,您不希望在用户导航时保持 DataReader 打开。 您可以将 DataReader 记录读入对象中。 或者,您可以将记录检索到 DataSet 中,并一次显示 DataTable 中的 DataRows。

我建议,如果可能的话,如果记录不是太多,请一次检索所有记录。 这将节省对数据库的重复调用。

另一方面,如果有很多记录,您可以检索前几条(例如 10 或 20 条),并且仅在用户单击超出初始记录集时通过新的数据库调用检索下一组记录。 这就是延迟加载。

In .Net, there are many ways to do this. One that I like is to use a DataReader, which can return multiple recordsets. You can loop through its records using While DataReader.Read.

One of the advantages of using DataReader is that it is a forward-only, read-only object, so it's fast and light-weight.

To allow the user to navigate through all of the records, one at a time, you do not want to hold a DataReader open while the user navigates. you can read the DataReader records into objects. Or, you can retrieve the records into a DataSet, and display the DataRows from the DataTable one at a time.

I would suggest that, if possible, you retrieve all of the records at once if there are not too many. This will save repeated calls to the database.

On the other hand, if there are a lot of records, you could retrieve the first few (say, 10 or 20) and only retrieve the next set of records with a new database call if the user clicks beyond the initial set. This is lazy loading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文