tags:

views:

39

answers:

3

i have a query that return only one row (always) and i want to convert this row to class object (lets say obi)

i have a feeling that using data table to this kind of query is to much but i dont realy know which other data object to use

data reader?

is there a way to execute sql command to data row ?

+2  A: 

DataReader is the best choice here - DataAdapters and DataSets may be overkill for a single row, although, that said, if performance is not critical then keeping-it-simple isn't a bad thing. You don't need to go from DataReader -> DataRow -> your object, just read the values off of the DataReader and you're done.

Will A
can you show me how ?
maggie
Check out the example on http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx - note that you can also use the fieldname rather than column ordinal when taking the value from the reader, e.g. dr("MyField") is fine.
Will A
A: 

A datareader lets you query individual fields. If you want the row as a single object, I believe the DataTable/DataRowView family of objects is in fact the way to go.

Nicolas78
arrrr i knew i will get 2 diffrent answers ... which one to choose ?
maggie
pls check my comment to your question. If you somehow need the DataRow object for your object creation or not - that's the question imo
Nicolas78
i have only one datarow .... if i need datarow or not .. that is part of the question :-)
maggie
A: 

You might seriously consider taking a look at Linq-to-Sql or Linq-to-Entities.

The appeal of these frameworks is they provide automatic serialization of your database data into objects, abstract away many of the mundane details of connection management, and have better compile-time support by providing strongly-typed properties which you can use without string keys or column ordinals.

When using Linq, the difference between retrieving a single row vs. retrieving multiple rows often only involves appending .Single() or .First() to your query.

At any rate, if you already use or are willing to learn one of these frameworks, you may see the bulk and difficulty of data access code reduce substantially.

With respect to DataReader vs. DataSet/DataTable, it is correct that it takes more cycles to allocate and populate a data table; however, I highly doubt you will notice the difference unless creating an extremely high volume of database calls.

In case it is helpful, here are documentation examples of data access using data readers and data sets.

kbrimington