473,382 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 29346
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thaddeus | last post by:
//Author: //Thaddeus Jacobs, MCP //Kinematic Automation, Inc. //mailto:tjacobs@kinematic.com // //Description: //convert ADO .NET dataset to ADO 2.5 2.6 2.7 recordset and v/v //DataSet to...
11
by: Vinod I | last post by:
Hi Team, I am having a string as "System.Data.SqlDbType.Int". Now I want to convert this string type to actual type to use with my Command object Parameter Creation. How I will convert this...
2
by: jiangyh | last post by:
hi there : I have a question about how to convert Type to DbType? thanks a lot. jiangyh
1
by: Stan Sainte-Rose | last post by:
Oopps sorry for the previous post. As I said, I m trying to make a web custom control and I do know how I have to convert this part to for the render section In fact it may be a stupid...
0
by: Harry Haller | last post by:
The context is shown below in the getGames() method. I get errors on these lines: dtGames.Rows = (TimeSpan)dtGames.Rows; dtGames.Rows = (DayOfWeek)dtGames.Rows; because the playDate column...
1
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the...
11
by: Matt F | last post by:
I'm trying to do something that seems like it should be pretty simple, but haven't found a solution. I am trying to add a datacolumn to a datatable that adds or subtracts a number of days based on...
3
by: DaveP | last post by:
im Trying to set a Datacolumn type for a new table boolean is not working, What is the correct waty //column.DataType = System.Type.GetType"System.Data.SqlTypes.SqlBoolean"); column.DataType...
0
by: SMH | last post by:
Hi All, I am currently learning .Net 2, studying for 70-528. I've hit a bit of a brick wall with DataColumn.Expression. As I understand it, this can be used to (For example) concatenate two...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.