sqldatareader的简单问题
在读取数据时,遇到空就会引发异常,请问怎么处理,我需要把非空字段收集起来,代码如下:
Conn.Open();
dr = sqlCommand.ExecuteReader();
DateTime dtnew = dt.AddMonths(1);
TimeSpan ts = dtnew.Subtract(dt);
for (int i = 0; i < ts.Days; i++)
{
if(dr.Read())
{
if (Convert.ToSingle(dr[0]) < 1000)
{
count += Convert.ToSingle(dr[0]);
}
}
}
------解决方案--------------------
Conn.Open();
dr = sqlCommand.ExecuteReader();
DateTime dtnew = dt.AddMonths(1);
TimeSpan ts = dtnew.Subtract(dt);
for (int i = 0; i < ts.Days; i++)
{
if(dr.Read())
{
if (dr[0]!= DBNull.Value && Convert.ToSingle(dr[0]) < 1000)
{
count += Convert.ToSingle(dr[0]);
}
}
}