Datareader和列表

时间:2012-11-15 14:32:54

标签: c# asp.net .net ado.net

我正在尝试创建如下列表,并使用datareader中的值更新它。我需要帮助编写代码来从数据读取器更新此列表。

internal IList<FilingDto> LoadStatusDtofromReader(IDataReader reader)
        {
            IList<FilingDto> filingstatus = new List<FilingDto>();

            while (reader !=null && reader.Read())
             {
                var dto = new FilingDto();
                var Year = (Decimal)reader["Year"];
                dto = new FilingDto()
                {

                    Controllerid = (Guid)reader["Collectorid"],
                    Status = DBNull.Value.Equals(reader["Status"]) ? string.Empty : reader["Status"].ToString(),
                    Year = Convert.ToInt32(Year),
                    Level = DBNull.Value.Equals(reader["Level"]) ? string.Empty : reader["ServiceLevel"].ToString()
                };

                filingstatus.Add(dto);
            }

            return status;
        }

从datareader读取的代码如下,但我卡在中间请帮我完成这个

DataTable FilingStatus = new DataTable("FilingStatus");
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString);
                sqlcon.Open();
                SqlCommand cmd = new SqlCommand("select Collectorid, Status, Year, Level from dbo.abc", sqlcon);

                using (IDataReader dr =  

3 个答案:

答案 0 :(得分:1)

试试这个,

    DataTable FilingStatus = new DataTable("FilingStatus");
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["CentralW2Database"].ConnectionString); 
    sqlcon.Open();
    SqlCommand cmd = new SqlCommand("select CollectorGuid, FileStatus,FilingYear, ServiceLevel from dbo.FilingRequestQueue", sqlcon);

    using (var dr = cmd.ExecuteReader()) 
    {
       IList<FilingDto> list = LoadStatusDtofromReader(dr);
    }

    internal IList<FilingDto> LoadStatusDtofromReader(IDataReader reader)
    {
        var filingstatus = new List<FilingDto>();

        while (reader != null && reader.Read())
        {
            var dto = new FilingDto
            {
                Controllerid = (Guid)reader["Collectorid"],
                Status = DBNull.Value.Equals(reader["Status"]) ? string.Empty : reader["Status"].ToString(),
                Year = Convert.ToInt32((Decimal)reader["Year"]),
                Level = DBNull.Value.Equals(reader["Level"]) ? string.Empty : reader["ServiceLevel"].ToString()
            };

            filingstatus.Add(dto);
        }

        return filingstatus;
    }

答案 1 :(得分:1)

您应该在SqlCommand对象上调用ExecuteReader

SqlCommand cmd = new SqlCommand("select Collectorid, Status, Year, Level from dbo.abc", sqlcon);
            SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    Console.WriteLine(String.Format("{0}, {1}",
                        reader[0], reader[1]));
                }
            }
            finally
            {
                // Always call Close when done reading.
                reader.Close();
            }

我建议使用ORM,比如实体框架而不是ADO.NET

答案 2 :(得分:0)

如果您可以重复使用原始功能,请执行

using (DataReader dr = cme.ExecuteReader()) 
{
    IList<FilingDto> list = LoadStatusDtofromReader(dr);
}