Results 1 to 2 of 2

Thread: SqlString Formater Class

  1. #1

    Thread Starter
    Junior Member jamison's Avatar
    Join Date
    Jan 2005
    Posts
    26

    Talking SqlString Formater Class

    This function generates a correctly formated string for use in a SQL Server query. I would appreciate any comments. I have actually added it to our MS-DBAB.


    Thanks, Jamie
    VB Code:
    1. '********************************************************************************
    2.     '* Function: SqlString
    3.     '* Description: Converts object to an appropriate sql string value
    4.     '* Dependent: SqlHelper
    5.     '* History:  
    6.     '* 01/12/2005 Jamie White Created
    7.     '*********************************************************************************
    8.     Public Shared Function SqlString(ByVal pObj As Object) As String
    9.         Dim strReturn, strType As String
    10.  
    11.         If (pObj Is Nothing) Then
    12.             Return "null"
    13.         End If
    14.  
    15.         If pObj Is DBNull.Value Then
    16.             Return "null"
    17.         End If
    18.  
    19.         strType = pObj.GetType().ToString
    20.         Select Case strType
    21.  
    22.             Case "System.Int32", "System.Int16", "System.Int64", "System.Decimal", "System.Double"
    23.                 'Numbers
    24.                 strReturn = CStr(pObj)
    25.  
    26.             Case "System.String"
    27.                 'Text
    28.                 strReturn = CStr(pObj)
    29.                 strReturn = "'" & strReturn.Replace("'", "''") & "'"
    30.  
    31.             Case "System.Date", "System.DateTime"
    32.                 'Dates
    33.                 strReturn = CStr(pObj)
    34.                 If strReturn = "12:00 AM" Or strReturn = "" Then
    35.                     strReturn = "null"
    36.                 Else
    37.                     strReturn = "'" & strReturn & "'"
    38.                 End If
    39.  
    40.             Case "System.Boolean"
    41.                 'Boolean
    42.                 If CBool(pObj) Then
    43.                     strReturn = "1"
    44.                 Else
    45.                     strReturn = "0"
    46.                 End If
    47.  
    48.             Case Else
    49.                 strReturn = "null"
    50.         End Select
    51.  
    52.         Return strReturn
    53.     End Function

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Re: SqlString Formater Class

    You could combine the first 2 if...then... statements.
    VB Code:
    1. If pObj Is Nothing OrElse pObj Is DBNull.Value Then
    2.    Return "Null"
    3. End IF
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width