Question

I am attempting to do as stated above combing two DataSet.tables into one table. I was wondering if it were possible to do so. I call my stored procedure and it returns my values I set them to a table. (All illustrated below).

Tried:

Adding both table names in the Mapping section ("Tables", IncomingProductTotals).

Adding both as one table in Mapping Section ("Tables", IncomingProductTotals1) ("Tables", TotalDownTimeResults1)

Doing lots of research most I can find is on sql joining tables.

SqlCommand cmd = new SqlCommand("L_GetTimeTotals", conn);
cmd.Parameters.Add("@startTime", SqlDbType.DateTime, 30).Value = RadDateTimePicker2.SelectedDate;
cmd.Parameters.Add("@endTime", SqlDbType.DateTime, 30).Value = RadDateTimePicker3.SelectedDate;
cmd.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(cmd);

var ds = new DataSet();

ds.Tables.Add("IncomingProductTotals");
ds.Tables.Add("IncomingProductTotalsA");
ds.Tables.Add("IncomingProductTotalsB");
ds.Tables.Add("IncomingProductTotals1");
ds.Tables.Add("IncomingProductTotalsA1");
ds.Tables.Add("IncomingProductTotalsB1");

da.TableMappings.Add("Table", "IncomingProductTotals");
da.TableMappings.Add("Table1", "IncomingProductTotalsA");
da.TableMappings.Add("Table2", "IncomingProductTotalsB");
da.TableMappings.Add("Table3", "IncomingProductTotals1");
da.TableMappings.Add("Table4", "IncomingProductTotalsA1");
da.TableMappings.Add("Table5", "IncomingProductTotalsB1");

da.Fill(ds);

ds.Tables.Remove("IncomingProductTotalsA");
ds.Tables.Remove("IncomingProductTotalsB");
ds.Tables.Remove("IncomingProductTotalsA1");
ds.Tables.Remove("IncomingProductTotalsB1");

ExcelHelper.ToExcel(ds, "TimeTotals.xls", Page.Response);
Était-ce utile?

La solution

Have you tried the Merge method? http://msdn.microsoft.com/en-us/library/system.data.datatable.merge(v=vs.110).aspx

foreach(var t in ds.Tables.Skip(1))
{
    t.Merge(ds.Tables[0]);
}

edit: Skip is Linq. You can use this too:

for(int i = 1; i < ds.Tables.Count - 1; i++) 
    ds.Tables[i].Merge(ds.Tables[0]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top