ananth reddy ananth reddy - 26 days ago 7
C# Question

Extracting all data from a DataSet

In my program of retrieving data from database for a certain criteria, I have created a method which returns a DataSet. I want to read all the rows present in that DataSet. I cannot use ExecuteReader in this context. A

foreach
loop is the right approach but I'm not able to figure out the syntax for that.

GetDataSet("SearchNumberSP", dictionary);
returns
DataSet d1;

The DataSet has only 2 columns. Please help me to figure this out!

Answer

A Dataset has between zero and (theoretically) infinite DataTables and those have between zero and (theoretically) infinite Datarows/DataColumns.

It doesn't make sense to say that the DataSet has only 2 columns because DataSet has no property called DataColumns. Thus, it doesn't make sense to return a Dataset if you're only looking for a single table, you should return a DataTable.

If you're insisting on returning a Dataset (which represents a full database), the code to parse that would look something like this:

foreach (DataTable table in _dataset)
{
    foreach (DataRow dataRow in table.Rows)
    {
         foreach(DataColumn dataColumn in table.Columns)
         {
              Console.Writeline([dataColumn].ToString());
         }
    }
}
Comments