我正在使用 SQLDataReader 从数据库中检索可能为空的值。我已经研究出如何处理 Null 字符串值,但无法获得相同的技巧来处理整数或布尔值:
Using cmd As DbCommand = store.GetStoredProcCommand("RetrievePOCO")
store.AddInParameter(cmd, "ID", DbType.Int32, ID)
Using reader As IDataReader = store.ExecuteReader(cmd)
If reader.Read() = True Then
Dim newPOCO As New POCO()
With newPOCO
'If the source column is null TryCast will return nothing without throwing an error
.StatusXML = TryCast(reader.GetString(reader.GetOrdinal("StatusXML")), String)
'How can a null integer or boolean be set elegantly?
.AppType = TryCast(reader.GetInt32(reader.GetOrdinal("AppType")), System.Nullable(Of Integer))
.Archived = TryCast(reader.GetBoolean(reader.GetOrdinal("Archived")), Boolean)
那么如何优雅地设置空整数或布尔值呢?我在 C# 中看到了建议,但它们没有正确转换为 VB,给出了“TryCast 操作数必须是引用类型,但是整数?是值类型的编译器错误。