合并到1数据表与相同的行数2数据表。 [英] Merging 2 datatables in to 1 datatable with same number of rows.

查看:116
本文介绍了合并到1数据表与相同的行数2数据表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何合并两个DataTable到同一行。我使用不同的存储过程来获取数据到数据集。在asp.net使用C#,我想将它们合并,以便有相同数量的行作为表1与添加列从表2中。

How can i merge two Datatables into the same row. I am using different stored procedures to get data into datasets. In asp.net using c#, i want to merge them so there are same number of rows as table 1 with an added column from table 2.

例如:

DataTable table1 = dsnew.Tables[0];
DataTable table2 = dsSpotsLeft.Tables[0];
table1.Merge(table2);

这是我取4行,而不是2行。我缺少的是在这里吗?提前致谢!!

This is fetching me 4 rows instead of 2 rows. What am i missing here? Thanks in advance!!

推荐答案

不知道更多有关这些表的设计,一些这是炒作。

Without knowing more about the design of these tables, some of this is speculation.

这听起来像您要执行的是加入。例如,如果你有一个表,该表是这样的:

What it sounds like you want to perform is a JOIN. For example, if you have one table that looks like:

StateId, StateName

和看起来像

EmployeeId, EmployeeName, StateId

和你想要的结果集,看起来像

and you want to end up with a result set that looks like

EmployeeId, EmployeeName, StateId, StateName

您将执行以下查询:

SELECT Employee.EmployeeId, Employee.EmployeeName, Employee.StateId, State.StateName
FROM Employee
INNER JOIN State ON Employee.StateId = State.StateId

这给你一个结果,但不更新任何数据。再次,猜测你的数据集,我假设你的Employee表的版本可能类似于结果集:

This gives you a resultset but doesn't update any data. Again, speculating on your dataset, I'm assuming that your version of the Employee table might look like the resultset:

EmployeeId, EmployeeName, StateId, StateName

Statename的需要被填充。在这种情况下,你可以写查询语句:

but with StateName in need of being populated. In this case, you could write the query:

UPDATE Employee
SET Employee.StateName = State.StateName
FROM Employee
INNER JOIN State ON Employee.StateId = State.StateId

经测试,在SQL Server中。

Tested in SQL Server.

这篇关于合并到1数据表与相同的行数2数据表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆