简体   繁体   English

将列添加到现有数据集中的最佳方法是什么?

[英]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分析地址

  • The order of the columns doesn't matter very much, but if it's not too much work, I would prefer the order above. 列的顺序无关紧要,但是如果工作量不大,我希望上面的顺序。
  • I would like to carry out mathematical operations on time1 and time2, and accordingly update the analysis column. 我想对time1和time2进行数学运算,并据此更新分析列。 Am I better off doing this in C# or JS? 我最好在C#或JS中这样做吗?

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.

相关问题 在C#中为现有代码添加自定义功能的最佳方法是什么? - What's the best way to add custom functionality to existing code in C#? 基于现有列将新列添加到数据表的最佳方法 - Best way to add a new column to a data table based on an existing column 将数据集 (.net) 中的列加载到字符串 [] 中的最佳方法是什么? - What is the best way to load a column from a dataset (.net) into a string[]? 最好的方法是在现有数据表中添加带有顺序编号的新列 - Best way add a new column with sequential numbering in an existing data table 更改ListBox的UniformGrid列数的最佳方法是什么? - What's the best way of changing a ListBox's UniformGrid column count? 向datagrid列按钮添加操作的最佳方法是什么? - What is the best way to add action to datagrid column button? 将可互换行为添加到ruby类的最佳方法是什么? - What's the best way to add interchangeable behavior to a ruby class? 在运行时在网格中添加ui控件的最佳方法是什么? - What's the best way to add an ui control in the grid at runtime? 将一项添加到 IEnumerable 的最佳方法是什么<T> ? - What's the Best Way to Add One Item to an IEnumerable<T>? 在另一个列表中添加列表副本的最佳方法是什么? - What's the best way to add a copy of a list in another list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM