值得參考的NULL替換值

來源:互聯網
上載者:User

Imports System

Imports

System.Reflection

Namespace

DotNetNuke

'*********************************************************************

'

' Null Class

'

' Class for dealing with the translation of database null values

'

'*********************************************************************

Public Class Null

' define application encoded null values

Public Shared ReadOnly Property NullInteger() As Integer

Get

Return -1

End Get

End Property

Public Shared ReadOnly Property NullDecimal() As Decimal

Get

Return Decimal.MinValue

End Get

End Property

Public Shared ReadOnly Property NullDate() As Date

Get

Return Date.MinValue

End Get

End Property

Public Shared ReadOnly Property NullString() As String

Get

Return ""

End Get

End Property

Public Shared ReadOnly Property NullBoolean() As Boolean

Get

Return False

End Get

End Property

Public Shared ReadOnly Property NullGuid() As Guid

Get

Return Guid.Empty

End Get

End Property

' sets a field to an application encoded null value ( used in Presentation layer )

Public Shared Function SetNull(ByVal objField As Object) As Object

If Not objField Is Nothing Then

If TypeOf objField Is Integer Then

SetNull = NullInteger

ElseIf TypeOf objField Is Single Then

SetNull = NullInteger

ElseIf TypeOf objField Is Double Then

SetNull = NullInteger

ElseIf TypeOf objField Is Decimal Then

SetNull = NullDecimal

ElseIf TypeOf objField Is Date Then

SetNull = NullDate

ElseIf TypeOf objField Is String Then

SetNull = NullString

ElseIf TypeOf objField Is Boolean Then

SetNull = NullBoolean

ElseIf TypeOf objField Is Guid Then

SetNull = NullGuid

Else

Throw New NullReferenceException

End If

Else ' assume string

SetNull = NullString

End If

End Function

' sets a field to an application encoded null value ( used in BLL layer )

Public Shared Function SetNull(ByVal objPropertyInfo As PropertyInfo) As Object

Select Case objPropertyInfo.PropertyType.ToString

Case "System.Int16", "System.Int32", "System.Int64", "System.Single", "System.Double"

SetNull = NullInteger

Case "System.Decimal"

SetNull = NullDecimal

Case "System.DateTime"

SetNull = NullDate

Case "System.String", "System.Char"

SetNull = NullString

Case "System.Boolean"

SetNull = NullBoolean

Case "System.Guid"

SetNull = NullGuid

Case Else

' Enumerations default to the first entry

Dim pType As Type = objPropertyInfo.PropertyType

If pType.BaseType.Equals(GetType(System.Enum)) Then

Dim objEnumValues As System.Array = System.Enum.GetValues(pType)

Array.Sort(objEnumValues)

SetNull = System.Enum.ToObject(pType, objEnumValues.GetValue(0))

Else

Throw New NullReferenceException

End If

End Select

End Function

' convert an application encoded null value to a database null value ( used in DAL )

Public Shared Function GetNull(ByVal objField As Object, ByVal objDBNull As Object) As Object

GetNull = objField

If objField Is Nothing Then

GetNull = objDBNull

ElseIf TypeOf objField Is Integer Then

If Convert.ToInt32(objField) = NullInteger Then

GetNull = objDBNull

End If

ElseIf TypeOf objField Is Single Then

If Convert.ToSingle(objField) = NullInteger Then

GetNull = objDBNull

End If

ElseIf TypeOf objField Is Double Then

If Convert.ToDouble(objField) = NullInteger Then

GetNull = objDBNull

End If

ElseIf TypeOf objField Is Decimal Then

If Convert.ToDecimal(objField) = NullDecimal Then

GetNull = objDBNull

End If

ElseIf TypeOf objField Is Date Then

If Convert.ToDateTime(objField) = NullDate Then

GetNull = objDBNull

End If

ElseIf TypeOf objField Is String Then

If objField Is Nothing Then

GetNull = objDBNull

Else

If objField.ToString = NullString Then

GetNull = objDBNull

End If

End If

ElseIf TypeOf objField Is Boolean Then

If Convert.ToBoolean(objField) = NullBoolean Then

GetNull = objDBNull

End If

ElseIf TypeOf objField Is Guid Then

If CType(objField, System.Guid).Equals(NullGuid) Then

GetNull = objDBNull

End If

Else

Throw New NullReferenceException

End If

End Function

' checks if a field contains an application encoded null value

Public Shared Function IsNull(ByVal objField As Object) As Boolean

If Not objField Is Nothing Then

If TypeOf objField Is Integer Then

IsNull = objField.Equals(NullInteger)

ElseIf TypeOf objField Is Decimal Then

IsNull = objField.Equals(NullDecimal)

ElseIf TypeOf objField Is Date Then

Dim objDate As DateTime = CType(objField, DateTime)

IsNull = objDate.Date.Equals(NullDate.Date)

ElseIf TypeOf objField Is String Then

IsNull = objField.Equals(NullString)

ElseIf TypeOf objField Is Boolean Then

IsNull = objField.Equals(NullBoolean)

ElseIf TypeOf objField Is Guid Then

IsNull = objField.Equals(NullGuid)

Else ' complex object

IsNull =

False

End If

Else

IsNull =

True

End If

End Function

End Class

End

Namespace

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.