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:
'******************************************************************************** '* Function: SqlString '* Description: Converts object to an appropriate sql string value '* Dependent: SqlHelper '* History: '* 01/12/2005 Jamie White Created '********************************************************************************* Public Shared Function SqlString(ByVal pObj As Object) As String Dim strReturn, strType As String If (pObj Is Nothing) Then Return "null" End If If pObj Is DBNull.Value Then Return "null" End If strType = pObj.GetType().ToString Select Case strType Case "System.Int32", "System.Int16", "System.Int64", "System.Decimal", "System.Double" 'Numbers strReturn = CStr(pObj) Case "System.String" 'Text strReturn = CStr(pObj) strReturn = "'" & strReturn.Replace("'", "''") & "'" Case "System.Date", "System.DateTime" 'Dates strReturn = CStr(pObj) If strReturn = "12:00 AM" Or strReturn = "" Then strReturn = "null" Else strReturn = "'" & strReturn & "'" End If Case "System.Boolean" 'Boolean If CBool(pObj) Then strReturn = "1" Else strReturn = "0" End If Case Else strReturn = "null" End Select Return strReturn End Function


Reply With Quote