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

时间:2023-03-08 17:32:09
有可能读出的数据为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"];