![](/img/trans.png)
[英]when i try to get the user name in c# i get this message <Converting null literal or possible null value to non-nullable type>
[英]The problem when I try to read nullable value in c#
我正在尝试读取我的数据库。 我有一列存储 int 值或空值。 当我尝试阅读本专栏时,我通常使用这行代码;
int Score = Convert.ToInt32(reader["Score"]);
但它可以为空。 所以我不能将它转换为int。 然后我尝试了;
bool tryGetScore = int.TryParse(reader["Score"], out player1Score);
但我也无法解析这个值。 它说“无法从 'Object' 转换为 'System.ReadOnlySpan'”
我怎样才能读取这个值。 每当我遇到空值时,我的程序就会崩溃
例如,您可以尝试使用isDbNull
int scoreIdx = reader.GetOrdinal("Score");
int score = reader.IsDbNull(scoreIdx) ?
0 : reader.GetInt32(scoreIdx);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.