tom tom - 2 months ago 6
C# Question

How to read data from SQL Server database using Data Set / Data Adapter?

I am trying to do a simple thing, display data from a table that is in a SQL Server database.

I used the Data Source tab to add a Data Set and configure the Data Adapter.

The structure in Data Sources tab looks like this :

MyDataSet
- MyTable
-- ColumnA
-- ColumnB
-- ColumnC


Now I would like to get all the values from
ColumnB
and write them to the console, basically
select ColumnB from MyTable
.

How do I do that using the Data Set / Data Adapter?

Answer

As per my understanding of your question, the below may help you:

foreach(DataRow dr in MyDataSet.Tables["MyTable"])
{
   Console.WriteLine(dr["ColumnB"].ToString());
}