Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading DataSet in C#

Tags:

c#

dataset

If we have filled a DataSet using a select query in C#, how can we read the column values?

I want to do something like this:

string name = DataSetObj.rows[0].columns["name"]

What would the correct syntax look like to achieve my goal?

like image 667
Riz Avatar asked Jul 14 '11 11:07

Riz


2 Answers

foreach(var row in DataSetObj.Tables[0].Rows)
{
    Console.WriteLine(row["column_name"]);
}
like image 55
mdm Avatar answered Sep 30 '22 19:09

mdm


If you already have a dataset, it's something like this;

object value = dataSet.Tables["MyTable"].Rows[index]["MyColumn"]

If you are using a DataReader:

using (SqlCommand cmd = new SqlCommand(commandText, connection, null))
{
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            testID = (int)reader["id"];
        }
    }
}
like image 24
JohnD Avatar answered Sep 30 '22 19:09

JohnD