[英]What's the best way to add a column to an existing dataset?
Okay, my description is a little long-winded but bear with me. 好吧,我的描述有些冗长,但请耐心等待。
I have two extremely long queries that I am running on multiple tables, using an INNER JOIN. 我有两个非常长的查询,使用INNER JOIN在多个表上运行。 The only difference between these two queries is that they take different input values for one of the WHERE clauses, which they get from two separate DropDownLists.
这两个查询之间的唯一区别是,它们为WHERE子句采用了不同的输入值,它们是从两个单独的DropDownLists获取的。 The results of these queries are identical, save for the time1/time2 columns.
这些查询的结果是相同的,只是保留了time1 / time2列。
string query1 = "select Name = t1.name, time1=(SELECT hours FROM table3 WHERE street=list1.selectedvalue), t2.address from [table1] t1 INNER JOIN [table2] t2 ON t1.param = t2.param ORDER BY Name";
string query2 = "select Name = t1.name, time1=(SELECT hours FROM table3 WHERE street=list2.selectedvalue), t2.address from [table1] t1 INNER JOIN [table2] t2 ON t1.param = t2.param ORDER BY Name";
(Please don't bother with the syntax of the queries above, I have executed the queries and they both return the expected results.) (请不要理会上面查询的语法,我已经执行了查询,它们都返回了预期的结果。)
I would like to merge these two tables into a single dataset (or any other structure than can be used as a datasource for a gridview). 我想将这两个表合并为一个数据集(或可以用作gridview的数据源的任何其他结构)。 The 'name' column is unique, some others might be too, I can check.
“名称”列是唯一的,我可以检查一下。 I want the final table columns to resemble
我希望决赛桌的栏位看起来像
Name Time1 Time2 Analysis Address 名称时间1时间2分析地址
Currently, I am using a datatable to collect the results of the two queries. 当前,我正在使用数据表来收集两个查询的结果。 I tried using the Datatable.Merge method, but that actually doubles the number of rows (kind of like an outer join) in spite of naming the columns the same (in the columns.Add(new DataColumn...)
我尝试使用Datatable.Merge方法,但这实际上使行数加倍(有点像外部联接),尽管将列命名为相同的列(在columns.Add(new DataColumn ...)中)
Thanks for looking :) I appreciate your input. 感谢您的帮助:)感谢您的投入。
Datatable dt1 = new datatable;
Datatable dt2 = new datatable;
dt1.Columns.Add(new DataColumn("name", typeof(string)));
dt1.Columns.Add(new DataColumn("time1", typeof(int)));
dt1.Columns.Add(new DataColumn("time2", typeof(int)));
dt1.Columns.Add(new DataColumn("analysis", typeof(string)));
dt1.Columns.Add(new DataColumn("address", typeof(string)));
SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read())
{
DataRow drow = new DataRow();
drow["name"] = dr["name"].ToString();
drow["time1"] = dr["time1"];
drow["address"] = dr["CurrentTime"].ToString();
dt1.Rows.Add(drow);
}
dr.Close();
dr = cmd2.ExecuteReader();
while (dr.Read())
{
DataRow drow = new DataRow();
drow["time2"] = dr["time1"];
drow["analysis"] = "I need javascript here";
dt2.Rows.Add(drow);
}
dr.Close();
dt1.Merge (dt2);
this.GridView1.DataSource = dt1;
this.GridView1.DataBind;
The answer can be found here. 答案可以在这里找到。 With a few tweaks, I got it to do exactly what I wanted.
经过一些调整,我可以完全按照自己的意愿进行操作。 http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/f122d7f4-3b7d-4d93-bd0f-8bb57cd990a4/
http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/f122d7f4-3b7d-4d93-bd0f-8bb57cd990a4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.