最近发现一段代码在处理空值上不是很好,分别写了2段代码,一个是我的,一个是兄弟的,哪个好点呢?

重构代码(2)-处理空字符串'<summary>将空字段转换为空串</summary>
重构代码(2)-处理空字符串'
<param name="Field">字段实例</param>
重构代码(2)-处理空字符串'
<return>字段内容</return>
重构代码(2)-处理空字符串
Public Function ConvertNull2String(Field As ADODB.Field) As Variant
重构代码(2)-处理空字符串    Require Me.IsInstance(Field), Me, 
"ConvertNull2String""Field必须实例化"
重构代码(2)-处理空字符串    
If Field.Type = adVarWChar Or Field.Type = adChar Then
重构代码(2)-处理空字符串        ConvertNull2String 
= Field.Value & ""
重构代码(2)-处理空字符串    
Else
重构代码(2)-处理空字符串        ConvertNull2String 
= Field.Value
重构代码(2)-处理空字符串    
End If
重构代码(2)-处理空字符串
End Function
重构代码(2)-处理空字符串
重构代码(2)-处理空字符串
'取得指定字段得值内容
重构代码(2)-处理空字符串
Public Function GetFieldValue(Field As Field, Optional IsNumeric As Boolean = FalseAs Variant
重构代码(2)-处理空字符串
On Error GoTo e
重构代码(2)-处理空字符串    
If Not Field Is Nothing Then
重构代码(2)-处理空字符串        
If IsNull(Field.Value) Then
重构代码(2)-处理空字符串            
If IsNumeric Then
重构代码(2)-处理空字符串                GetFieldValue 
= 0
重构代码(2)-处理空字符串            
Else
重构代码(2)-处理空字符串                GetFieldValue 
= ""
重构代码(2)-处理空字符串            
End If
重构代码(2)-处理空字符串        
Else
重构代码(2)-处理空字符串            GetFieldValue 
= Field.Value
重构代码(2)-处理空字符串        
End If
重构代码(2)-处理空字符串    
Else
重构代码(2)-处理空字符串        GetFieldValue 
= ""
重构代码(2)-处理空字符串    
End If
重构代码(2)-处理空字符串    
重构代码(2)-处理空字符串    
Exit Function
重构代码(2)-处理空字符串e:
重构代码(2)-处理空字符串    GetFieldValue 
= ""
重构代码(2)-处理空字符串
End Function 

当然如果在Sql语句里边处理好了,就更棒了,向下边这样:

SELECT A, ISNULL(B, '0') AS B FROM tNulls

相关文章:
http://www.access-cn.com/Article/Class5/Class28/Class30/200508/1744.html
http://www.zdnet.com.cn/developer/database/story/0,3800066906,39412365,00.htm
http://unruledboy.cnblogs.com/archive/2004/06/27/18988.aspx

相关文章:

  • 2021-04-08
  • 2022-01-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-12-17
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
相关资源
相似解决方案