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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AlanRank #669 · 3.6K reputationPosted Jun 19, 2007, 6:04 AM EDT
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();
}
}
it novice9 reputationPosted Jun 20, 2007, 6:49 AM EDT
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
AlanRank #669 · 3.6K reputationPosted Jun 20, 2007, 7:47 AM EDT
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 novice9 reputationPosted Jun 20, 2007, 10:27 PM EDT
Thanks