从数据集中的4个数据表中检索第一行. [英] Retrieve first row from 4 datatables in a dataset.

查看:84
本文介绍了从数据集中的4个数据表中检索第一行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个数据集,其中包含4个数据表,每个数据表中都有一些记录.我需要从这4个表中选择最上面的一个记录并将其保存在数据表或任何数据集中,并在我的网格中显示这4条记录.请帮助我如何使用c#在asp.net中进行操作.请帮助我


i have a dataset which has 4 datatables in it and some records in each datatable.i need to select oly the top one record from these 4 tables and save it in either a datatable or any dataset and display these 4 records in my grid. pls help me how to do it in asp.net using c#.pls help me

推荐答案

尝试以下代码.

try the following code.

DataTable dt1_new = new DataTable();
dt1_new.Rows.Add(dt1.AsEnumerable().FirstOrDefault());
dt1_new.Rows.Add(dt2.AsEnumerable().FirstOrDefault());
dt1_new.Rows.Add(dt3.AsEnumerable().FirstOrDefault());
dt1_new.Rows.Add(dt4.AsEnumerable().FirstOrDefault());


DataTable 获取第一条记录并不是一件容易的事.只需访问第0行即可.看起来像这样:
Getting the first record from a DataTable is not a hard thing to do. Simply access row 0 only. That would look something like this:
var row = DataSet.DataTables[0].Rows[0];


这将为您提供一个DataRow ,其中包含DataSet中第一个表中的第一条记录.您可以为四个表中的每个表执行此操作.然后,您可以创建一个新的DataTable 并将这四行插入其中.但是,这里的关键是四行必须具有匹配的列名称和类型.这是您手动创建DataTable的方法:
http://www.dotnetperls.com/datatable [


That will give you a DataRow that contains the first record from the first table in the DataSet. You could do this for each of your four tables. Then you could create a new DataTable and insert these four rows into it. The key here though would be that the four rows would have to have matching column names and types. Here is how you would manually create a DataTable:
http://www.dotnetperls.com/datatable[^]

The bigger question I see here though is why are you do it this way? It seems like you could at least limit your query to only give you the first row in each table (SELECT TOP 1 * FROM table). You might also be able to do the joining of the four entries at the database level as well. Something like this:

SELECT TOP 1 FieldA, FieldB FROM table1
UNION ALL
SELECT TOP 1 FieldA, FieldB FROM table2
UNION ALL
SELECT TOP 1 FieldA, FieldB FROM table3
UNION ALL
SELECT TOP 1 FieldA, FieldB FROM table4


这将为您提供每个表中的最高记录,并将它们连接在一起.输出将是四行.

显然,这些最后的建议取决于您的环境设置方式,但如果可能的话,我绝对建议您采用这种方式.


That would give you the top record from each table and join them together. The output would be four rows.

Obviously these last suggestions would be dependent on how your environment is set up, but if possible I would definitely recommend going this way.


这篇关于从数据集中的4个数据表中检索第一行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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