PC Review


Reply
Thread Tools Rate Thread

Converting between DbType and System.Type

 
 
James Moore
Guest
Posts: n/a
 
      19th Sep 2004
Attempting to convert from a System.Type to a DbType:

public static System.Data.DbType ToDbType (System.Type type)

{

object dummyInstance = Activator.CreateInstance (type);

// Note that we don't care what type of XXParameter we use; the

// parameter is going to get thrown away. Basically, we're

// creating a pseudointerface to code hidden inside the XXParameter

// constructor.

MySql.Data.MySqlClient.MySqlParameter dummyParameter = new MySqlParameter
("dummyname", dummyInstance);

// This also does something sensible:
//
// System.Data.SqlClient.SqlParameter p = new
System.Data.SqlClient.SqlParameter ("dummyname", dummyInstance);
//
// However, the ODBC and OLE versions bite the wax tadpole and usually just
return String as the DbType


return dummyParameter.DbType;

}

However, when I plug in different parameter types (OleDbParameter,
OdbcParameter),
I get surprising results. Specifically, the DbType field of the
dummyParameter
object is usually String, no matter what gets passed in as the object value.
System.Data.SqlClient.SqlParameter behaves sensibly; I get back DbTypes that
make sense.

I'm not sure if it's a bug, or if ODBC and OLE just don't really care, so
they
don't return useful values. If all you're planning to do is something like

escape_all_sql_special_characters (dummyParameter.Value.ToString ())

in the driver, then the "type" of the parameter is probably irrelevant to
you.

OK, just took another look at the doc; OLE and ODBC have different ways of
doing things.
Specifically, the two-argument (string, object) version of the constructor
for OleDbParameter expects

string - the name of the parameter
value - "When you specify an Object in the value parameter, the OleDbType is
inferred from the .NET Framework type of the Object."

That's what the MySQL version does as well.

ODBC is weird:

string - the same (the name of the parameter)
value - An OdbcParameter object.

Huh? I can't see how initializing a parameter with another parameter as its
value makes sense; either I'm very confused or the
doc is wrong. The wording of the rest of the OdbcParameter (string, object)
documentation is so similar to the Ole and Sql version
that I'm tempted to think the doc is wrong.

- James
(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting string to system.type John Dann Microsoft VB .NET 2 22nd Dec 2008 11:38 PM
converting system.datatype to DbType Chukkalove Microsoft ADO .NET 0 18th Jul 2006 11:26 AM
How to convert Type to DbType? jiangyh Microsoft C# .NET 2 11th Mar 2005 12:23 AM
convert a Type to DBType ?! franky Microsoft Dot NET Compact Framework 2 9th Apr 2004 06:39 PM
How to convert System.Type to Data.DbType? Cepheus Microsoft ADO .NET 1 17th Mar 2004 09:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:30 AM.