Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Bordeman - 15 Nov 2006 21:32 GMT
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.
Chester - 16 Nov 2006 09:34 GMT
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

> 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
[quoted text clipped - 3 lines]
>
> Chris B.
Chris Bordeman - 16 Nov 2006 18:54 GMT
Gotcha, thanks for the mappings.

> Hi Chris Bordeman,
>
[quoted text clipped - 45 lines]
>>
>> Chris B.
Rahul - 18 Nov 2006 04:47 GMT
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,
>
[quoted text clipped - 43 lines]
> >
> > Chris B.
Rahul - 18 Nov 2006 04:48 GMT
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,
>
[quoted text clipped - 43 lines]
> >
> > Chris B.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.