Retrieving values from a dataset

  • Thread starter Thread starter Sorin Sandu
  • Start date Start date
S

Sorin Sandu

How can I retrieve values from a dataset.
I want to store values from a dataset into some variables.
 
Hi Sorin,

The most simple example

mytextbox.text = dataset1.tables(0).rows(0).item(0).ToString

The first item in your first row from your first table from a dataset.

Cor
 
Hi Sorin:

Cor provided a really nice implementation but let me just add something if I
may. You can reference the row like this Tables[0].Rows[0][1] where the
first argument is the Row index and the second is the column index. I think
Item[1] makes it more clear but that's a matter personal preference.
However, you'll note that he uses numeric indexes and one of the overloads
for it allows you to do this with the Column name and some people opt for it
because it's more 'clear'. So you could do this Rows[0]["ColumnName"] or
..Item["ColumnName"] . If you are dealing with just a few rows this isn't a
big deal, but each time you reference it with a name, the runtime has to
resolve the Ordinal value and this can hinder performance if you are dealing
with a lot of rows. If you had five columsn for instance, and 1000 rows, at
each pass it would have to resolve it five times. The DataReader has a
..GetOrdinal method that allows you to only resolve this once so you can do
something like int FirstName = DataReader.GetOrdinal[0] where to 0 column
was named FirstName in the database. There's no such method in DataTables.
However, Bill Vaughn has a really cool concept where you create an enum and
use Readable names which correspond to the index:

public enum ColumnNames : uint{
FirstName
,LastName
}

Then, you can use DataSet.Tables[0].Rows[0].[FirstName];

and you'll get the best of both worlds.

HTH,

Bill
 
Back
Top