472,374 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to convert Type to DbType?

hi there :

I have a question about how to convert Type to DbType?

thanks a lot.
jiangyh
Nov 16 '05 #1
2 29215
Hi,

You don't need to do explicit conversion from Type to SqlType for example
Int32 to SqlInt32.

SqlInt32 sqlint;
int i = 20;

sqlint = i; // no explicit conversion required...

It is automatically taken care for you by the SqlType. The SqlType
implements implicit operator overloading to take for the same.

Hope that helps...

Regards,
Madhu

MVP - C# | MCSD.NET

"jiangyh" wrote:
hi there :

I have a question about how to convert Type to DbType?

thanks a lot.
jiangyh

Nov 16 '05 #2
I'm not sure why someone hasn't written this, but here is goes:

(no warranties, expressed or implied)
/// <summary>
/// Get the native type based on the database type
/// </summary>
/// <param name="dbType">The database type to convert</param>
/// <returns>The equivalent managed type, otherwise the DBNull
type</returns>
static Type ConvertType( DbType dbType )
{
Type toReturn = typeof( DBNull ) ;

switch( dbType )
{
case DbType.String:
toReturn = typeof( string ) ;
break ;

case DbType.UInt64 :
toReturn = typeof( UInt64 ) ;
break ;

case DbType.Int64 :
toReturn = typeof( Int64 ) ;
break ;

case DbType.Int32:
toReturn = typeof( Int32 ) ;
break ;

case DbType.UInt32:
toReturn = typeof( UInt32 ) ;
break ;

case DbType.Single:
toReturn = typeof( float ) ;
break ;

case DbType.Date:
toReturn = typeof( DateTime) ;
break ;

case DbType.DateTime :
toReturn = typeof( DateTime ) ;
break ;

case DbType.Time :
toReturn = typeof( DateTime ) ;
break ;

case DbType.StringFixedLength:
toReturn = typeof( string ) ;
break ;

case DbType.UInt16:
toReturn = typeof( UInt16 ) ;
break ;

case DbType.Int16:
toReturn = typeof( Int16 ) ;
break ;

case DbType.SByte:
toReturn = typeof( byte ) ;
break ;

case DbType.Object:
toReturn = typeof( object ) ;
break ;

case DbType.AnsiString :
toReturn = typeof( string ) ;
break ;

case DbType.AnsiStringFixedLength :
toReturn = typeof( string ) ;
break ;

case DbType.VarNumeric :
toReturn = typeof( decimal) ;
break ;

case DbType.Currency:
toReturn = typeof( double ) ;
break ;

case DbType.Binary :
toReturn = typeof( byte[] ) ;
break ;

case DbType.Decimal :
toReturn = typeof( decimal ) ;
break ;

case DbType.Double:
toReturn = typeof( Double ) ;
break ;

case DbType.Guid :
toReturn = typeof( Guid ) ;
break ;

case DbType.Boolean :
toReturn = typeof( bool ) ;
break ;
}

return toReturn ;
}

jiangyh wrote:
hi there :

I have a question about how to convert Type to DbType?

thanks a lot.
jiangyh


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Davor | last post by:
This is what I want to do: int currentWord = 0; currentWord++; textBoxOtherLang.Text = orginalLanguage; But, I get a "Cannot implictly convert type 'char' to 'string'" error when trying to...
22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
14
by: Chris | last post by:
Hi, I try to print out truth-tables for an &&-operation using the following code, unfortunatly I get compiler errors : for ( byte i1=0; i1<=1; i1++) { for ( byte i2=0; i2<=1; i2++) { bool...
2
by: Patrick Olurotimi Ige | last post by:
When i convert:- this code from VB to C# Why do i get error "Cannot implicitly convert type 'object' to 'bool' VB --- If cmdcommand.Parameters("ReturnValue").Value = 1 Then lblStatus.Text =...
9
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient:...
6
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web...
6
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
7
by: =?Utf-8?B?RWl0YW4=?= | last post by:
What would be the "right" way to convert type 'byte' to 'string'? Thanks EitanB
2
by: ransaj | last post by:
objCompanyRequirement.Salary = Convert.ToInt32 (txtCRF_Salary.Text); cannot implicitly convert type 'int' to 'string' objCompanyRequirement.InterviewDate = ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.