473,385 Members | 1,356 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,385 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 29449
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 = ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.