使用odbcdatareader获取最后一条记录 [英] get last record using odbcdatareader

查看:111
本文介绍了使用odbcdatareader获取最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想从odbcdatareader中找到最后一条记录.我试图找到movelast方法,但是找不到.请向我建议解决方案.

这是代码:
我需要MoveLast和MoveFirst的相同代码.

Hi All,

I want to find last record from odbcdatareader.I tried to find movelast method but failed to find.Please suggest me solution.

Here is code:
I want same code for MoveLast and MoveFirst.

public bool MoveNext(Object reader)
       {
           OdbcDataReader rs = null;
           rs = (OdbcDataReader)reader;
           //Check if OdbcDataReader is valid or not
           if (rs != null)
           {

               try
               {
                   //Move to next record
                   return (rs.Read());

               }
               catch (Exception Ex)
               {
                   return false;
               }
           }
           return false;
       }

推荐答案

首先,这不是数据读取器的工作方式.方法Read的返回只是用于停止读取的布尔值.问题是:您不解释要返回的记录"是什么.除了阅读器本身之外,没有任何数据类型可以表示代码中的记录".您所能做的就是从某个位置的读取器(查询产生的数据集的行)中获取数据.您可以为每列使用Item属性或方法GetData,其中通过读取属性FieldCount找到了if的数量.

您可以创建一些数据容器来保存记录数据的副本,但是为什么要这样做呢?您可以改用System.Data.DataTableSystem.Data.TypedDataTable数据类型,请参见:
http://msdn.microsoft.com/en-us/library/system.data. datatable.aspx [^ ],
http://msdn.microsoft.com/en-us/library/bb358258.aspx [ ^ ].

另外,您没有随机访问记录的权限.您可以只读.如果需要第一条记录,则应该在第一次读取后记住一些数据.诸如"MoveFirst"或"MoveLast"之类的方法将毫无意义.

请参阅:
http://msdn.microsoft.com/en-us/library/system. data.common.dbdatareader.aspx [ ^ ].

更重要的是,最后"记录的概念也没有多大意义.正式地,您可以使用数据读取器进行读取,并且最终他们使用数据读取器实例来获取记录信息.称其为最后一个",但仅在特定情况下才正式出现在最后.数据库本身不支持表中记录的任何特定顺序,并且数据读取器检索到的表"实际上并不存在-它是查询的人工产物.您可以通过SQL子句"ORDER BY"控制顺序,但是如果不这样做,则数据读取器附带的数据顺序是不确定的.您确定您确实需要最后一条记录吗?

我建议更好地学习有关ADO.NET工作原理的一些更基本的想法.这是一篇很好的CodeProject文章:
为初学者使用ADO.NET [
First of all, this is not how data reader works. The return of the method Read is just the Boolean value used to stop reading. The problem is: you don''t explain what is the "record" to return. There is no a data type which represents a "record" in your code, except the reader itself. All you can do is getting the data from the reader at some position (row of the data set produced by a query); you can use Item properties or the method GetData for every column, where the number of if found by reading the property FieldCount.

You can create some data container to hold a copy of the record data, but why doing all that? You can use the data types System.Data.DataTable or System.Data.TypedDataTable instead, please see:
http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx[^],
http://msdn.microsoft.com/en-us/library/bb358258.aspx[^].

Also, you don''t have a random access to the records. You can read forward-only. If you need the first record, you should remember some data after the very first read, that''s it. The methods like "MoveFirst" or "MoveLast" would not make sense.

Please see:
http://msdn.microsoft.com/en-us/library/system.data.common.dbdatareader.aspx[^].

More importantly, the notion of "last" record also makes no much sense. Formally, you can read using the data reader to the very end and them use the data reader instance to get the record information. Call it "last", but it is last only formally, only for a given case. The database itself does not support any specific order of the records in the table, and the "table" retrieved by a data reader does not physically exists — it''s an artifact of the query. You can control the order by the SQL clause "ORDER BY", but if you don''t, the order of data which comes with data reader is undefined. Are you sure you really need "that" last record.

I would recommend to learn better some more basic ideas on how ADO.NET works. This is a very good CodeProject article:
Using ADO.NET for beginners[^].

—SA


这篇关于使用odbcdatareader获取最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆