So in that case, the easier way to go is simply to keep your code as is.
Then remove this line:
//create dataSet for Leads
DataSet dsLeads = new DataSet();
and replace it with this line
//add the table
dsClicks.Tables.Add(leadsTable);
So now, you will have a single dataset, dsClicks that contains two
datatables - clicks, and leads. You can now efficiently loop thru the
dataset picking whatever datatable is required. Also, now that you just have
one dataset to manipulate, the code is less resource intensive.
foreach(datatable dt in dsClicks)
{
//this will allow you to iterate your tables
foreach(datarow dr in dt)
{
//this will allow you to loop thru the rows in each datatable
}
}
for direct access, simply use this:
dsClicks.Datatables[1].Rows[0][1].Tostring() returns column 1 of row 1
or
dsClicks.Datatables["clicks"].Rows[0][1].Tostring() returns column 1 of row
1
Now, the other path forward would be to not use the query to go grab
results - for instance a count of the rows - you can just simply use a
filter to count the rows or examine it like so:
dsClicks.Datatables["clicks"].Rows.Count. This saves you an expensive sql
query. Another thing you can do
is sum or total/apply formulas and filters to these tables instead of doing
it thru a query.
int size = clickTable.Compute( "COUNT(column_name_to_count)",
"" ).ToString() ;
There are a lot of functions, roughly equivalent to SQL functions, that the
dataset/datatable can use as well.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------
Basically..Here is my code...can you assist with merging?
I am creating a dataset for each query, the only common field is
track_id, which I need to group the results by for each track_id.
protected void Page_Load(object sender, EventArgs e)
{
string dataBaseName;
string startDate;
string endDate;
dataBaseName = (string) Session["repositoryID"];
startDate = (string) Session["startDate"];
endDate = (string) Session["endDate"];
dataBaseName = dataBaseName.Trim();
// create & configure the connection to database
string strConnString =
ConfigurationManager.ConnectionStrings["" + dataBaseName +
""].ConnectionString;
SqlConnection theConnection = new SqlConnection(strConnString);
//create dataSet for Clicks
DataSet dsClicks = new DataSet();
DataTable clickTable = new DataTable("clicks");
//create the column(s)
DataColumn trackID = clickTable.Columns.Add("track_id",
typeof(string));
DataColumn uniqueClicks =
clickTable.Columns.Add("unique_clicks", typeof(string));
DataColumn totalClicks =
clickTable.Columns.Add("total_clicks", typeof(string));
//set the primary key
clickTable.PrimaryKey = new DataColumn[] { trackID };
//add the table
dsClicks.Tables.Add(clickTable);
//create the command and adapter
SqlDataAdapter clickAdapter = new SqlDataAdapter();
SqlCommand clickCommand = new SqlCommand(
" SELECT track_id, count(distinct ip) as unique_clicks,
count(ip) as total_clicks "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = 'C' "
+ "AND record_created >= '" + startDate + "' "
+ "AND record_created <= '" + endDate + "' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
clickAdapter.SelectCommand = clickCommand;
//fill the dataTable
clickAdapter.Fill(dsClicks, "unique_clicks");
//create dataSet for Leads
DataSet dsLeads = new DataSet();
DataTable leadsTable = new DataTable("leads");
//create the column(s)
DataColumn trackIDLead = leadsTable.Columns.Add("track_id",
typeof(string));
DataColumn leads = leadsTable.Columns.Add("leads",
typeof(string));
//set column(s) properties
//set the primary key
leadsTable.PrimaryKey = new DataColumn[] {trackIDLead};
//add the table
dsLeads.Tables.Add(leadsTable);
//create the command and adapter
SqlDataAdapter leadsAdapter = new SqlDataAdapter();
SqlCommand leadsCommand = new SqlCommand(
"SELECT track_id, count(distinct ip) as leads "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = 'L' "
+ "AND record_created >= '" + startDate + "' "
+ "AND record_created <= '" + endDate + "' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
leadsAdapter.SelectCommand = leadsCommand;
//fill the dataTable
leadsAdapter.Fill(dsLeads, "leads");
}