Windows Develop Bookmark and Share   
 index > Windows Forms General > How to convert System.Type to Data.DbType?
 

How to convert System.Type to Data.DbType?

Hi,

Is it possible to convert a System.Type to Data.DbType (this is unfortunately an enumaeration)?

Thanks, Rainer.
MigrationUser 1  Tuesday, March 09, 2004 3:06 PM
a little quick/sloppy, but works...    

(de-typoed)

//---------------------------------------------------

using System;
using System.Data;
using System.Data.SqlClient;


          Class DBTypeConversion
                 {
private static String[,] DBTypeConversionKey = new String[,] 
{
 {"BigInt","System.Int64"},
 {"Binary","System.Byte[]"},
 {"Bit","System.Boolean"},
 {"Char","System.String"},
 {"DateTime","System.DateTime"},
 {"Decimal","System.Decimal"},
 {"Float","System.Double"},
 {"Image","System.Byte[]"},
 {"Int","System.Int32"},
 {"Money","System.Decimal"},
 {"NChar","System.String"},
 {"NText","System.String"},
 {"NVarChar","System.String"},
 {"Real","System.Single"},
 {"SmallDateTime","System.DateTime"},
 {"SmallInt","System.Int16"},
 {"SmallMoney","System.Decimal"},
 {"Text","System.String"},
 {"Timestamp","System.DateTime"},
 {"TinyInt","System.Byte"},
 {"UniqueIdentifer","System.Guid"},
 {"VarBinary","System.Byte[]"},
 {"VarChar","System.String"},
 {"Variant","System.Object"}
};


public static SqlDbType SystemTypeToDbType( System.Type sourceType )
{
SqlDbType result;
String SystemType = sourceType.ToString();
String DBType = String.Empty;
int keyCount = DBTypeConversionKey.GetLength(0);

for(int i=0;i<keyCount;i++)
{
if(DBTypeConversionKey[i,1].Equals(SystemType)) DBType = DBTypeConversionKey[i,0];
}

if (DBType==String.Empty) DBType = "Variant";

result = (SqlDbType)Enum.Parse(typeof(SqlDbType), DBType);

return result;
}

public static Type DbTypeToSystemType( SqlDbType sourceType )
{
Type result;
String SystemType = String.Empty;
String DBType = sourceType.ToString();
int keyCount = DBTypeConversionKey.GetLength(0);

for(int i=0;i<keyCount;i++)
{
if(DBTypeConversionKey[i,0].Equals(DBType)) SystemType = DBTypeConversionKey[i,1];
}

if (SystemType==String.Empty) SystemType = "System.Object";

result = Type.GetType(SystemType);

return result;
}

                      }
MigrationUser 1  Thursday, April 22, 2004 11:09 PM
{"Timestamp","System.DateTime"}
****! It is incorrect, and the class returned for datetime in my DAL byte[]. I worked for 2 days on this problem.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
MigrationUser 1  Friday, April 01, 2005 9:08 AM

A simpler way:

// From Type to DBType

internal static DbType GetDbType(Type type)

{

String name = type.Name;

DbType val = DbType.String; // default value

try

{

val = (DbType)Enum.Parse(typeof(DbType), name, true);

}

catch (Exception)

{

// add error handling to suit your taste

}

return val;

}

djonexx  Tuesday, November 06, 2007 9:37 AM

You can use google to search for other answers

Custom Search

More Threads

• Anyone can help with axMediaPlayer ???
• mdichild anchoring controls and layout
• How to persist UI state between executions?
• How to detect global idle user time?
• Control.Focus & Control.Select
• MessageLoop Management in Application.Run - Help!
• KeyCode to KeyChar
• Control TextChanged Event Question
• PropertyGrid - property text value cannot be deleted using the DELETE key?
• Re: How to implement copy/paste/cut/undo/redo commands in C#?