How to Convert System.Type to DBType
Can some one please tell how to convert System.Type to DBType. Please response as soon as possible
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
it novicePosted Jun 21, 2007, 2:27 AM
Thanks
AlanPosted Jun 20, 2007, 11:47 AM
You could shorten the code quite a bit using my earlier approach. This would also deal with unsigned types as well:
public static DbType TypeToDbType(Type t)
{
DbType dbt;
try
{
dbt = (DbType)Enum.Parse(typeof(DbType), t.Name);
}
catch
{
dbt = DbType.Object;
}
return dbt;
}
it novicePosted Jun 20, 2007, 10:49 AM
public
static DbType TypeToDbType(Type T){
switch (T.FullName){
case "System.Int64": return DbType.Int64; break; case "System.Int32": return DbType.Int32; break; case "System.Int16": return DbType.Int16; break; case "System.Decimal": return DbType.Decimal; break; case "System.Double": return DbType.Double; break; case "System.Boolean": return DbType.Boolean; break; case "System.String": return DbType.String; break; case "System.DateTime": return DbType.DateTime; break; default : return DbType.Object; break;}
}
But I am looking for some better solution then this. Thanks
AlanPosted Jun 19, 2007, 10:04 AM
Do you mean something like this?
using System;
using System.Data;
class Test
{
static void Main()
{
Type t = typeof(System.Decimal); // or whatever
DbType dbt = (DbType)Enum.Parse(typeof(DbType), t.Name);
Console.WriteLine(dbt.ToString()); //Decimal
Console.ReadLine();
}
}