如何通过List 而不是数据表从sql中检索数据?

时间:2021-11-08 21:16:10

How to retrieve data from Database by ( List<> ) instead of data table?

如何通过(List <>)而不是数据表从数据库中检索数据?

How can I do this?

我怎样才能做到这一点?

1 个解决方案

#1


0  

Follow these steps

按着这些次序

  1. create a class emulating your table
  2. 创建一个模拟表格的类

  3. declare the list
  4. 声明清单

  5. retrieve data and bind it to the list
  6. 检索数据并将其绑定到列表

  7. display your data

    显示您的数据

    public class myListClass { public string myListClassItem1{ set; get; } public string myListClassItem2{ set; get; } public string myListClassItem3{ set; get; } }

    public class myListClass {public string myListClassItem1 {set;得到; public string myListClassItem2 {set;得到; public string myListClassItem3 {set;得到; }}

    List items = new List();

    List items = new List();

    while (reader.Read()) { items.Add(new myListClass(reader["Item1"].ToString(), reader["Item2"].ToString(), reader["Item3"].ToString())); }

    while(reader.Read()){items.Add(new myListClass(reader [“Item1”]。ToString(),reader [“Item2”]。ToString(),reader [“Item3”]。ToString())) ; }

    myDataGrid.DataSource = items;

    myDataGrid.DataSource = items;

    myDataGrid.DataBind();

#1


0  

Follow these steps

按着这些次序

  1. create a class emulating your table
  2. 创建一个模拟表格的类

  3. declare the list
  4. 声明清单

  5. retrieve data and bind it to the list
  6. 检索数据并将其绑定到列表

  7. display your data

    显示您的数据

    public class myListClass { public string myListClassItem1{ set; get; } public string myListClassItem2{ set; get; } public string myListClassItem3{ set; get; } }

    public class myListClass {public string myListClassItem1 {set;得到; public string myListClassItem2 {set;得到; public string myListClassItem3 {set;得到; }}

    List items = new List();

    List items = new List();

    while (reader.Read()) { items.Add(new myListClass(reader["Item1"].ToString(), reader["Item2"].ToString(), reader["Item3"].ToString())); }

    while(reader.Read()){items.Add(new myListClass(reader [“Item1”]。ToString(),reader [“Item2”]。ToString(),reader [“Item3”]。ToString())) ; }

    myDataGrid.DataSource = items;

    myDataGrid.DataSource = items;

    myDataGrid.DataBind();