如何在Dataadapter的帮助下填充数据库中的通用列表? [英] How Do I Fill Generic List From Database Just Like Fill Datatable With The Help Of Dataadapter?
本文介绍了如何在Dataadapter的帮助下填充数据库中的通用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何填写列表< string>从数据库就像在数据适配器的帮助下填充datatable一样?
我想优化我的页面,我想用List替换数据表。我如何存档这个?
i希望在asp.net中使用List作为控件的dataSource。
解决方案
让我们说,
class 学生
{
public int ID
{ get ; set ; }
public string 名称
{获得跨度>; set ; }
}
现在你已经有了DataTable,遍历下面的所有记录并将其存储在列表中
,
列表< Student> stud = new List< Student>();
foreach (DataRow dr in dt.Rows) // dt是一个DataTable
{
stud.Add( new 学生{ID = int .Parse(dr [ Id]。ToString()),Name = dr [ 名称 ]的ToString()});
}
返回 stud;
-KR
数据适配器仅适用于数据表和数据集。如果您想使用自己的类与泛型集合,请查看适当的ORM,例如Entity框架。这里有很多文章可以帮助您入门:
实体框架
绝对初学者实体框架简介
how do i fill list<string> from database just like fill datatable with the help of data adapter?
i want to optimize my page and i want to replace the datatable with List. how do i archive this?
i want to use List as dataSource of the control in asp.net.
解决方案
Let's say,
class Student { public int ID { get; set; } public string Name { get; set; } }
Now you've got DataTable, iterate through all records like below and store it in alist
,
List<Student> stud = new List<Student>(); foreach(DataRow dr in dt.Rows) // dt is a DataTable { stud.Add(new Student{ ID = int.Parse(dr["Id"].ToString()), Name = dr["Name"].ToString()}); } return stud;
-KR
Data adapters work with Data tables and Data sets only. If you want to use your own classes with generic collections have a look on a proper ORM like Entity framework for example. There are plenty of articles around here to get you started:
Introduction to Entity Framework
An Introduction to Entity Framework for Absolute Beginners
这篇关于如何在Dataadapter的帮助下填充数据库中的通用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文