By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

How to convert Type to DbType?

P: n/a
hi there :

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

thanks a lot.
jiangyh
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.