简体   繁体   English

如果默认情况下字符串为空,为什么我无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象?

[英]If strings are nullable by default, why do i get Unable to cast object of type 'System.DBNull' to type 'System.String'.?

I have seen similar threads like this where the solution is a tertiary if. 我也有类似的线程像这样在解决方案是一个三级如果。

My question is, why even get such an error if Strings are nullable? 我的问题是,如果字符串可为空,为什么还要得到这样的错误?

I am reading a value of a text column in access using ado.net. 我正在使用ado.net读取访问中的文本列的值。 Whenever there is a row with an empty text column i get that error. 每当有一行带有空文本列的行时,我都会收到该错误。

Culprit: 罪魁祸首:

while (dr.Read())
{
      UserList.Add(new UserInfo()
      {
        .
          DestributionGroup = (string)dr["Destribution Group"]
        .
      }
}
class UserInfo
{
.
    public string DestributionGroup;
.
}

Edit: 编辑:

So in other words I have to convert all of my strings that I am reading from the DB into a line similar to this? 所以换句话说,我必须将我从数据库读取的所有字符串转换成与此类似的行?

return (accountNumber == DBNull.Value) ? string.Empty : accountNumber.ToString ()

No other way around it? 没有其他办法吗?

Because null != DBNull.Value . 因为null != DBNull.Value

But you can check if it the value in the DataReader is null with the IsDbNull method: 但是您可以使用IsDbNull方法检查DataReader的值是否为空:

DestributionGroup = dr.IsDbNull("Destribution Group") ? "" : dr.GetString("Destribution Group");

如果您不介意在dr [“ Destribution Group”]为DBNull.Value时将DestributionGroup设置为null,则可以使用此方法:

DestributionGroup = dr["Destribution Group"] as string;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象 - How can i fix Unable to cast object of type 'System.DBNull' to type 'System.String` System.InvalidCastException:无法将类型为System.DBNull的对象转换为类型为System.String的对象 - System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String' 转换:无法将“System.DBNull”类型的对象转换为“System.String”类型 - Transformation: Unable to cast object of type 'System.DBNull' to type 'System.String' RowDataBound:从dataTable获取值! 无法将“System.DBNull”类型的对象强制转换为“System.String”类型 - RowDataBound : Getting value from dataTable ! Unable to cast object of type 'System.DBNull' to type 'System.String' 无法将“System.DBNull”类型的对象转换为 Scaffold-DbContext .Net Core 中的“System.String”类型 - Unable to cast object of type 'System.DBNull' to type 'System.String' in Scaffold-DbContext .Net Core Entity Framework Group BY 导致无法将“System.DBNull”类型的对象转换为“System.String”类型 - Entity Framework Group BY causing Unable to cast object of type 'System.DBNull' to type 'System.String' 获取所有产品时无法将类型为“System.DBNull”的 object 转换为类型“System.String” - Unable to cast object of type 'System.DBNull' to type 'System.String' On getting all Products 未以根身份连接时,无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.DBNull' to type 'System.String' WHEN NOT CONNECTING AS ROOT 无法将“System.DBNull”类型的 object 转换为类型“System.String” - Unable to cast object of type 'System.DBNull' to type 'System.String` db查询:一个字段可以是null,另一个不是“无法将'System.DBNull'类型的object转换为'System.String'类型” - db query: one field ok to be null, another not “Unable to cast object of type 'System.DBNull' to type 'System.String'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM