System.Data.SqlTypes.SqlNullValueException: 数据为空。不能对空值调用此方法或

有可能读出的数据为NULL,可以这样改:

方法一:
while (reader.Read())
{
for (int i = 0; i < 7; i++)
{
if (reader.IsDBNull(i))
{
continue;
}
readstring[i] = reader.GetString(i);
}
}
方法二:

string sql = "select ISNULL([Name],''),ISNULL([Address],''),ISNULL([WebAddress],''),ISNULL([Telephone],''),ISNULL([Description],''),ISNULL([LeftTopPic],''),ISNULL([LeftBottomPic],'') from [t_Customer] where [CustomerID] = " + ctid;

方法三:
while (reader.Read())
{
for (int i = 0; i < 7; i++)
{
readstring[i] = reader[i].ToString();
}
}

用 is DBNull 

如:
user.Phone = reader["phone"] is DBNull ? "" : (string)reader["phone"];
posted on 2013-12-18 14:24  yun007  阅读(3917)  评论(0编辑  收藏  举报