Given an SQL query like this
select a.id, b.id, a.columnA,a.columnB,a.itemno,b.itemno
from table1 a
join table2 b on a.id=b.id
Your C# code would/could look like this to read all rows and all columns:
using (SqlCommand getAllColumns = new SqlCommand("select a.id, b.id,a.columnA,a.columnB,a.itemno,b.itemno from table1 a join table2 b on a.id=b.id", conn))
{
using (var drreader = getAllColumns.ExecuteReader())
{
DataTable tb = new DataTable();
tb.BeginLoadData();
tb.Load(drreader);
tb.EndLoadData();
foreach(DataRow row in tb.Rows.Cast<DataRow>().ToList())
{
// assuming these are all varchar columns
string idA = (string)row["id"];
string idB = (string)row["id1"];
string columnA = (string)row["columnA"];
string columnB = (string)row["columnB"];
string columnAItemNo = (string)row["itemno"]; //fetches the first itemno column, a.itemno in this case
string columnBItemNo = (string)row["itemno1"]; //fetches the second itemno column, b.itemno in this case
}
}
}
I use this on .NET Framework 4.5. If you want to verify or debug this, put a breakpoint on the foreach line and inspect the DataTable object. The second itemno column should be titled differently compared to the first one.