我在网格视图中绑定数据库中的数据。但在数据源“CountryList”中,仅从数据读取器添加偶数行。这段代码有什么问题?
using (cmd)
{
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
while (dr.Read())
{
CountryList.Add(ParseDataReader(dr));
}
cn.Close();
return CountryList;
}
}
}
Parse 方法是:
public static Countries ParseDataReader(SqlDataReader dr)
{
Countries MyCountries = new Countries();
dr.Read();
if (dr["CountryID"]!=DBNull.Value )
{
MyCountries.CountryID = (int)dr["CountryID"];
}
if (dr["Code"]!=DBNull.Value)
{
MyCountries.Code = dr["Code"].ToString();
}
if (dr["Name"]!=DBNull.Value)
{
MyCountries.Name = dr["Name"].ToString();
}
if (dr["RequiresState"]!= DBNull.Value)
{
MyCountries.RequiresState = (bool)dr["RequiresState"];
}
if (dr["Order"]!=DBNull.Value)
{
MyCountries.Order = (decimal)dr["Order"];
}
return MyCountries;
}
仅供参考:我的存储过程返回正确的行数。但数据读取器仅从结果中读取偶数行。