Get SqlDbType

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to get a SqlDbType from a string?

like:
SqlDbType myType = Type.GetType("SqlDbType.int");

You can do this for framework types but couldn't figure out how to do it for
sql types.

thanks,
 
Hi Chuck,

Base on my experience, "SqlDbType myType = Type.GetType("SqlDbType.int")"
won't work for two reasons.
1) System.Data.SqlDbType.Int isn't a type. System.Data.SqlDbType is a type
(an enumeration) and Int is a member of this enumeration.
2) Type.GetType ("System.Data.SqlDbType") will return null, because the
type lives in System.Data.dll, not mscorlib or the assembly the code will
be running in.We need to specify the full assembly name to get types from
other assemblies.
e.g:
Type.GetType("System.Data.SqlDbType, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089");

However, in your case, I think we can use the following statement to get
type "System.Data.SqlDbType.Int".
SqlDbType myType= (SqlDbType)Enum.Parse(typeof(SqlDbType), "Int");

If there is anything unclear, please don't hesitate to contact me.
I'm very glad to work with you.
Sincerely,
WenYuan
 
thanks,
that enum parsing sure comes in helpfull!
I had to add an ignore case to the parse;SQL server's internal tables store
everything in lower case.
 
You are welcome.
If there is anthing I can help with, please feel free to reply me.
I'm glad to work with you.

Sincerely,
Wen Yuan
 
Back
Top