convert DataColumn.GetType() into a DbType or SqlDbType?

  • Thread starter Thread starter Chris Bordeman
  • Start date Start date
C

Chris Bordeman

I have a DataColumn, want to derive the DbType. I can do column.GetType()
but that's a system type, not a db type. How do I convert it to the
corresponding type?

Thanks much!

Chris B.
 
Hi Chris Bordeman,

Its not possible to get the DBType via GetType(). You need to have a mapping
to get the relevent type. For eg,

private static Hashtable dbTypeTable;

private SqlDbType ConvertToDbType(Type t)
{
if(dbTypeTable == null)
{
dbTypeTable = new Hashtable();
dbTypeTable.Add(typeof(System.Boolean), SqlDbType.Bit);
dbTypeTable.Add(typeof(System.Int16), SqlDbType.SmallInt);
dbTypeTable.Add(typeof(System.Int32), SqlDbType.Int);
dbTypeTable.Add(typeof(System.Int64), SqlDbType.BigInt);
dbTypeTable.Add(typeof(System.Double), SqlDbType.Float);
dbTypeTable.Add(typeof(System.Decimal), SqlDbType.Decimal);
dbTypeTable.Add(typeof(System.String), SqlDbType.VarChar);
dbTypeTable.Add(typeof(System.DateTime), SqlDbType.DateTime);
dbTypeTable.Add(typeof(System.Byte[]), SqlDbType.VarBinary);
dbTypeTable.Add(typeof(System.Guid), SqlDbType.UniqueIdentifier);
}
SqlDbType dbtype;
try
{
dbtype = (SqlDbType)dbTypeTable[t];
}
catch
{
dbtype = SqlDbType.Variant;
}
return dbtype;
}

can be used to get the type.

Cheers,
Chester
 
Gotcha, thanks for the mappings.

Chester said:
Hi Chris Bordeman,

Its not possible to get the DBType via GetType(). You need to have a
mapping
to get the relevent type. For eg,

private static Hashtable dbTypeTable;

private SqlDbType ConvertToDbType(Type t)
{
if(dbTypeTable == null)
{
dbTypeTable = new Hashtable();
dbTypeTable.Add(typeof(System.Boolean), SqlDbType.Bit);
dbTypeTable.Add(typeof(System.Int16), SqlDbType.SmallInt);
dbTypeTable.Add(typeof(System.Int32), SqlDbType.Int);
dbTypeTable.Add(typeof(System.Int64), SqlDbType.BigInt);
dbTypeTable.Add(typeof(System.Double), SqlDbType.Float);
dbTypeTable.Add(typeof(System.Decimal), SqlDbType.Decimal);
dbTypeTable.Add(typeof(System.String), SqlDbType.VarChar);
dbTypeTable.Add(typeof(System.DateTime), SqlDbType.DateTime);
dbTypeTable.Add(typeof(System.Byte[]), SqlDbType.VarBinary);
dbTypeTable.Add(typeof(System.Guid), SqlDbType.UniqueIdentifier);
}
SqlDbType dbtype;
try
{
dbtype = (SqlDbType)dbTypeTable[t];
}
catch
{
dbtype = SqlDbType.Variant;
}
return dbtype;
}

can be used to get the type.

Cheers,
Chester


Chris Bordeman said:
I have a DataColumn, want to derive the DbType. I can do
column.GetType()
but that's a system type, not a db type. How do I convert it to the
corresponding type?

Thanks much!

Chris B.
 
Hi Chester,
I was also trying to do the same in my application. But i got stuck
with few things.

Like if you have a currency column or a nvarchar column the gettype
function will return you decimal and string respectively.

Just wanted to check if you know have a work around for this.

Thanks,
Rahul
Hi Chris Bordeman,

Its not possible to get the DBType via GetType(). You need to have a mapping
to get the relevent type. For eg,

private static Hashtable dbTypeTable;

private SqlDbType ConvertToDbType(Type t)
{
if(dbTypeTable == null)
{
dbTypeTable = new Hashtable();
dbTypeTable.Add(typeof(System.Boolean), SqlDbType.Bit);
dbTypeTable.Add(typeof(System.Int16), SqlDbType.SmallInt);
dbTypeTable.Add(typeof(System.Int32), SqlDbType.Int);
dbTypeTable.Add(typeof(System.Int64), SqlDbType.BigInt);
dbTypeTable.Add(typeof(System.Double), SqlDbType.Float);
dbTypeTable.Add(typeof(System.Decimal), SqlDbType.Decimal);
dbTypeTable.Add(typeof(System.String), SqlDbType.VarChar);
dbTypeTable.Add(typeof(System.DateTime), SqlDbType.DateTime);
dbTypeTable.Add(typeof(System.Byte[]), SqlDbType.VarBinary);
dbTypeTable.Add(typeof(System.Guid), SqlDbType.UniqueIdentifier);
}
SqlDbType dbtype;
try
{
dbtype = (SqlDbType)dbTypeTable[t];
}
catch
{
dbtype = SqlDbType.Variant;
}
return dbtype;
}

can be used to get the type.

Cheers,
Chester


Chris Bordeman said:
I have a DataColumn, want to derive the DbType. I can do column.GetType()
but that's a system type, not a db type. How do I convert it to the
corresponding type?

Thanks much!

Chris B.
 
Hi Chester,
I was also trying to do the same in my application. But i got stuck
with few things.

Like if you have a currency column or a nvarchar column the gettype
function will return you decimal and string respectively.

Just wanted to check if you know any work-around for this.

Thanks,
Rahul
Hi Chris Bordeman,

Its not possible to get the DBType via GetType(). You need to have a mapping
to get the relevent type. For eg,

private static Hashtable dbTypeTable;

private SqlDbType ConvertToDbType(Type t)
{
if(dbTypeTable == null)
{
dbTypeTable = new Hashtable();
dbTypeTable.Add(typeof(System.Boolean), SqlDbType.Bit);
dbTypeTable.Add(typeof(System.Int16), SqlDbType.SmallInt);
dbTypeTable.Add(typeof(System.Int32), SqlDbType.Int);
dbTypeTable.Add(typeof(System.Int64), SqlDbType.BigInt);
dbTypeTable.Add(typeof(System.Double), SqlDbType.Float);
dbTypeTable.Add(typeof(System.Decimal), SqlDbType.Decimal);
dbTypeTable.Add(typeof(System.String), SqlDbType.VarChar);
dbTypeTable.Add(typeof(System.DateTime), SqlDbType.DateTime);
dbTypeTable.Add(typeof(System.Byte[]), SqlDbType.VarBinary);
dbTypeTable.Add(typeof(System.Guid), SqlDbType.UniqueIdentifier);
}
SqlDbType dbtype;
try
{
dbtype = (SqlDbType)dbTypeTable[t];
}
catch
{
dbtype = SqlDbType.Variant;
}
return dbtype;
}

can be used to get the type.

Cheers,
Chester


Chris Bordeman said:
I have a DataColumn, want to derive the DbType. I can do column.GetType()
but that's a system type, not a db type. How do I convert it to the
corresponding type?

Thanks much!

Chris B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top