How do I best convert a string representation into a DbType?
Question
Suppose I have a string 'nvarchar(50)', which is for example the T-SQL string segment used in creating a table of that type. How do I best convert that to an enum representation of System.Data.DbType?
Could it handle the many different possible ways of writing the type in T-SQL, such as:
[nvarchar](50)
nvarchar 50
@Jorge Table: Yes, that's handy, but isn't there a prebaked converter? Otherwise good answer.
Solution
Hope this mapping table do the job.
OTHER TIPS
My first attempt would involve using a regex to parse the two parts of the declaration (where the second part is only used for variably sized types.) Make sure that you convert the type-name to lower case when you've parsed it.
You could make an enum with all the various types in it (lower-cased), then use Enum.Parse to get an instance of the enum value, and then use a switch-case to get the appropriate System.Data.DbType for each enum value.
Kind of gross, I admit.