469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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

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.
Nov 15 '06 #1
4 28752
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" wrote:
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.
Nov 16 '06 #2
Gotcha, thanks for the mappings.

"Chester" <ch*******@community.nospamwrote in message
news:08**********************************@microsof t.com...
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" wrote:
>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.

Nov 16 '06 #3
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

Chester wrote:
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" wrote:
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.

Nov 18 '06 #4
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

Chester wrote:
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" wrote:
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.

Nov 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Thaddeus | last post: by
2 posts views Thread by jiangyh | last post: by
1 post views Thread by Stan Sainte-Rose | last post: by
1 post views Thread by neeraj | last post: by
3 posts views Thread by DaveP | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.