Click here to Skip to main content
Licence CPOL
First Posted 8 Dec 2006
Views 40,675
Bookmarked 23 times

Convert System.Type to SqlDbType

By | 8 Dec 2006 | Article
Convert any compatible System.Type to SqlDbType.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This is a generic implementation of converting any compatible System.Type to SqlDbType for use in parameter objects.

Background

I wanted a generic method to convert system data types into a format which can be given to Parameter objects. After a lot of online search, I came across only some crude implementations which involved elaborate Select Case constructs.

Here is my implementation:

Private Function GetDBType(ByVal theType As System.Type) As SqlDbType
    Dim p1 As SqlClient.SqlParameter
    Dim tc As System.ComponentModel.TypeConverter
    p1 = New SqlClient.SqlParameter()
    tc = System.ComponentModel.TypeDescriptor.GetConverter(p1.DbType)
    If tc.CanConvertFrom(theType) Then
        p1.DbType = tc.ConvertFrom(theType.Name)
    Else
        'Try brute force
        Try
            p1.DbType = tc.ConvertFrom(theType.Name)
        Catch ex As Exception
            'Do Nothing
        End Try
    End If
    Return p1.SqlDbType
End Function

Highlights

This code uses intrinsic converters available in most objects. The object TypeConverter is the key in this case. These converters are also used by the system to persist data in XML files. The method GetConverter of TypeDescriptor retrieves the TypeConverter associated with the target object for which conversion is to be performed.

Simply use this function where needed. It will get you the SqlDbType equivalent of the System.Type passed to it as far as possible; else, by default, it will return the String data type equivalent. For SqlDbType, it gives NVarChar.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Girish Chandra

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 Pinmemberdojohansen2:35 9 Nov '09  
QuestionI cannot use this method on Compact Framework, Please Help Me PinmemberrizaSHSHKA1:17 23 May '09  
AnswerRe: I cannot use this method on Compact Framework, Please Help Me Pinmemberdonkhuan7:33 22 Dec '09  
GeneralC# version of the code Pinmembermcsjr5:58 19 Aug '08  
AnswerTry SqlMetaData PinmemberTheMega0:03 5 May '08  
GeneralI need to Convert from DbType to .NET Type PinmemberAIbanezW20:22 20 Jan '08  
Your article is interesting and useful.
I am trying to find a method for convert
DbType to .NET Type but now I did not found
 
I will appreciate your help so much.
 
Tnanks,
 
Ing. Aarón Ibáñez Werthermänn
IBM Certified Specialist for RUP
Microsoft Certified Trainer
MCSD .NET, MCAD

QuestionWhy throw out the exception? Pinmembervarnk10:52 21 May '07  
Generaldoesn't work with byte array Pinmemberpindurav20:34 18 Feb '07  
QuestionRe: doesn't work with byte array [modified] PinmemberTheMega22:24 29 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 8 Dec 2006
Article Copyright 2006 by Girish Chandra
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid