0

我正在尝试创建如下列表并使用数据读取器中的值对其进行更新。我在编写代码以从数据阅读器更新此列表时需要帮助。

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;
        }

从数据读取器读取的代码如下,但我被困在两者之间,请帮我完成这个

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 =  
4

3 回答 3

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;
    }
于 2012-11-15T14:39:01.333 回答
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

于 2012-11-15T14:41:49.070 回答
0

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

using (DataReader dr = cme.ExecuteReader()) 
{
    IList<FilingDto> list = LoadStatusDtofromReader(dr);
}
于 2012-11-15T15:25:38.600 回答