ADO.NET记录导航
我已经在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有必要回到过去糟糕的日子。 如果你能给出一个伪代码示例,我可以帮你翻译成vb.net。
这是一种通用的方法。
每次编辑 1:用户将导航结果,而不是查询。 ,您想要做的是 a) 获取结果并仅向它们显示 ds.Tables.Row() 的当前 rowindex,或者 b) 在每次导航时执行新查询(这不是一个真正性能良好的选项。)
因此 评论:不,他们没有。 但用户通常不会像这样与数据库交互工作。 您将需要获取结果的数据集/表,并使用按钮从数据集/表中检索相关行。
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.
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.
这一切都取决于用途:
如果您只需要列出一个或多个查询的结果,则应该使用数据读取器。 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
这是使用数据读取器的一个简单示例:
Here's a quick example of using the datareader:
在.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.