想要将其创建/转换为“类型列表”。并没有这样做过。一些好的例子会很棒。 [英] Would like to create/convert this into a "Typed List" and havent done this before. Some good examples would be great.

查看:49
本文介绍了想要将其创建/转换为“类型列表”。并没有这样做过。一些好的例子会很棒。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(dbDataReader.GetValue(0) + "," + dbDataReader.GetValue(1) + "," + dbDataReader.GetValue(2) + "," + dbDataReader.GetValue(3) + "," + dbDataReader.GetValue(4) + "," + dbDataReader.GetValue(5) + "," + dbDataReader.GetValue(6) + "," + dbDataReader.GetValue(7) + "," +
dbDataReader.GetValue(8) + "," + dbDataReader.GetValue(9) + "," +
dbDataReader.GetValue(10) + "," + dbDataReader.GetValue(11) + "," +
dbDataReader.GetValue(12) + "," + dbDataReader.GetValue(13) + "," +
dbDataReader.GetValue(14) + "," + dbDataReader.GetValue(15) + "," +
dbDataReader.GetValue(16) + "," + dbDataReader.GetValue(17) + "," +
dbDataReader.GetValue(18) + "," + dbDataReader.GetValue(19) + "," +
dbDataReader.GetValue(20) + "," + dbDataReader.GetValue(21) + "," +
dbDataReader.GetValue(22) + "," + dbDataReader.GetValue(23) + "," +
dbDataReader.GetValue(24) + "," + dbDataReader.GetValue(25) + "," +
dbDataReader.GetValue(26) + "," + dbDataReader.GetValue(27) + "," +
dbDataReader.GetValue(28) + "," + dbDataReader.GetValue(29) + "," +
dbDataReader.GetValue(30) + "," + dbDataReader.GetValue(37) + "," + dbDataReader.GetValue(38) + "," + dbDataReader.GetValue(39));





推荐答案

创建班级将所有属性作为select语句中的列,您需要为属性类型与相关数据库列类型匹配。例如,如果你有varchar列使用字符串属性..

在你的while循环之前使用上面的类创建列表

Create Class with All the properties as your columns in the select statement, you need to give property types match with relevant database column types. for example if you have varchar column use string property..
Create List before your while loop as below using above class
List<myclass> items = new List<myclass>();



在while循环中你可以创建新类的对象并将属性值设置如下


inside while loop you can create object of your new class and set the property values as below

while (dbDataReader != null && dbDataReader.Read())
{
    Myclass item = new Myclass();
    item.Name = dbDataReader.GetString(0);
    // set all other properties 
    //and finally add the object to List 
    items.Add(item);
}


这篇关于想要将其创建/转换为“类型列表”。并没有这样做过。一些好的例子会很棒。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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