con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
string userData;
userData = reader[0] is DBNull ? "RolesError" : reader.GetString(0);
userData += reader[1] == DBNull.Value? "|IdentityError" : "|" + reader.GetByte(1);
userData += reader.GetValue(2) is DBNull ? "|LastLoginError" : "|" + reader.GetDateTime(2);
userData += reader.GetValue(3) == DBNull.Value ? "|LastIPError" : "|" + reader.GetString(3);
reader.Close();
}
红字部分为判断数据库字段是否为DBNull 如果不判断会出现异常