我正在使用SQLDataReader从数据库检索可能为空的值。我已经学会如何处理空字符串值,但无法用同样的技巧处理整数或布尔值:
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操作数必须是引用类型,但整数?是值类型”的编译错误。