DbDataAdapter.Fill() - just the first row

W

William \(Bill\) Vaughn

If you use a DataReader, you can use the CommandBehavior.SingleRow.


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
C

Cor Ligthert [MVP]

David,

Will you explain this a little bit more, because when you want to load
database information in a datatable, than trying to do this withouth SQL is
at the moment impossible. In SQL is the simple First 1 so doing that extra
is as well crazy.

Cor
 
M

Miha Markic

Hi David,

You have various Fill method overloads that accept startRecord and
maxRecords argument.
However, this way is not optimal as all records will be fetched and only the
first one will be inserted into the table.
Best way is to specify the right sql statement...
BTW, did you consider an ORM based solution?
 
C

Charles Zhang

You can create a datareader object by using "ExecuteReader". To return
the first row only, using CommandBehavior.SingleRow

To fill a datatable from a datareader object, you can use DataTable.Load()

Charles Zhang
SpeedyDB Technologies (The fastest, most secure ADO.NET Provider over
Wide Area Network)
http://www.speedydb.com
 
M

Miha Markic

CommandBehavior.SingleRow is just an optimization hint and it doesn't affect
the actual select - meaning that all the data is fetched but only one row is
returned.
 
C

Charles Zhang

David said:
But no way to have DataTable.Load() load just the first row :(
The datareader only contains one row if you use
CommandBehavior.SingleRow to return the datareader. The DataTable.Load()
will only load whatever the datareader object returns.

Charles Zhang
SpeedyDB Technologies (The fastest, most secure ADO.NET Provider over
Wide Area Network)
http://www.speedydb.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top