如何在C#中从数据集中拆分特定表? [英] How to split particular table from dataset in C#?
本文介绍了如何在C#中从数据集中拆分特定表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据集中有多个数据表示例数据集中的五个数据表。现在我想拆分特定的数据表。如何实现它。
我尝试了什么:
我尝试使用名称拆分数据表。
i have multiple data table in dataset example five data tables in dataset.now i want to split particular data table.how to do it.
What I have tried:
I have tried split data table using name.
推荐答案
如果你想将表与数据集分开,那么拿一个新的数据表并给它分配这样的东西dt = ds.Tables [n]。
如果你想要来自特定表的一些记录,创建一个新的数据表并从数据集的表中添加新行。
If you want to separate the table from dataset, then take a new datatable and assign it something like this dt = ds.Tables[n].
If you want some records from a particular table, the create a new datatable and add new rows from the dataset's table.
这是我在项目中做的一些事情。在下面的示例中,我将从不同的表User和Roles获取数据集。一个我的存储过程被执行,我有我的数据集我正在使用
Here is some thing I do in my project. In the below example I am getting dataset from different tables User and Roles. One my stored procedure is executed and I have my dataset I am using
ds.Tables[0].AsEnumerable()
- 获取数据从数据表到userinfo。此外,我使用
- to get the data from data table in to userinfo. Further I am using
ds.Tables[1].AsEnumerable()
来访问数据集中的第二个表。您也可以尝试这样做。
to get access to the second table in the dataset. You can try to do in your case as well.
DataSet ds = dm.ExecuteQuery("SpGetUserDetails", parameters);
User userInfo = (from r in ds.Tables[0].AsEnumerable()
select new User
{
FullName = r["FirstName"].ToString() + " " + r["LastName"].ToString(),
FirstName = r["FirstName"].ToString(),
MiddleName = r["MiddleName"].ToString(),
LastName = r["LastName"].ToString(),
EmailId = r["EmailId"].ToString()
}).SingleOrDefault();
userInfo.Roles =(from r in ds.Tables[1].AsEnumerable()
select new UserRoles
{
UserRoleMappingId = Convert.ToInt32(r["UserRoleMappingId"].ToString()),
UserRoleId = Convert.ToInt32(r["UserRoleId"].ToString()),
UserId = Convert.ToInt32(r["UserId"].ToString()),
RoleName = r["RoleDescription"].ToString()
}).ToList();
希望这会有所帮助。
快乐学习
Hope this helps.
Happy Learning
这篇关于如何在C#中从数据集中拆分特定表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文