While CommitReader.Read()
Yield New Commit() With {
.FirstValue = CommitReader.GetInt32(CommitReader.GetOrdinal("FirstValue")),
.SecondValue = CommitReader.GetString(CommitReader.GetOrdinal("SecondValue")).Trim(),
'Lots of values
End While
我知道我可以做这样的事情;然而,有24个属性,我想尽可能使这个部分干净是否有一个整洁干净的方式来处理带有收益率的空值?
While CommitReader.Read()
new Commit(){
Dim index As Integer = reader.GetOrdinal("FirstValue")
If reader.IsDBNull(index) Then
FirstValue = String.Empty
Else
FirstValue = reader(index)
End If
index = reader.GetOrdinal("SecondValue")
If reader.IsDBNull(index) Then
SecondValue = String.Empty
Else
SecondValue = reader(index)
End If
}
End While
有没有更好的方式来处理这种类型的事情?我主要是一个C#开发人员,所以如果语法有点遗憾,我会用VB来支持它。
如果要将数据库NULL值转换为空字符串,可以在查询中执行此操作,例如, 'SELECT COALESCE(FirstValue,'')...'。 –
@AndrewMorton,当你的解决方案工作的时候,你需要注意到这个更多关于你想在哪里保存这个逻辑的问题 - 在数据库层或业务逻辑层。如果你把它放在数据库层中,那么你的业务逻辑假定数据库层总是返回空字符串 – Fabio
@Fabio是的,我同意丢失列数据为NULL的信息可能是一个问题。 –