DataType

J

Jim Heavey

I am having some difficulty figuring out how to pass the "right" dataType
to create a Data Parameter.

Suppose that I have created a data Parameter and set it's datatype, now I
want to use that same dataType to create another parmameter somewhere else
in the program. My problem is that if I us the "DataColumn.DataType" which
returns the "type" of the column, I am unable to figure out how to use this
type to create my "new" parameter.

So here is the signature of the method that I am calling to create my new
Parameter...

public IDbDataParameter CreateDataParameter
(string parameterName, Type dataType, string sourceColumn,
DataRowVersion version)

When I pass the following to this method
(Type) dc.DataType
I get an error message indicating that "Can not convert 'Type" to
System.Data.DbType"

If I just attempt to pass dc.DbType i get the error of "Can not convert
System.Type to System.Data.DbType"
DbType typ = (System.Data.DbType) dc.DataType;

What do I need to do to pass the correct type to this method?
 
M

Miha Markic

Hi Jim,

Since DbType and Type are two differenct things, you won't be able cast them
from one to other.
I wonder why does your CreateDataParameter method takes Type as parameter
instead of DbType.
 
P

Plausible Indirection

To the best of my understanding, a Type refers to a definition for a
class or structure whereas a DbType is an enumeration that typically
is more or less correlated to a provider specific enumeration like
SqlType or OracleType. That is why you can't cast or pass one as the
other; as Miha says, they are different things.

You most likely want to create your parameters using the provider type
enumeration that is tailored to your provider. For instance, if you
are
instantiating SqlParameters, you most likely want to pass in the
appropriate
SqlType into the constructor.

You can get the provider type using the, appropriately named,
ProviderType
column of the DataSet returned from IDataReader.GetSchemaTable(). I'm
not
sure offhand if there is another way to get the ProviderType
information.

Hope that helps,
Chris
 
M

Miha Markic

Hi,

Plausible Indirection said:
To the best of my understanding, a Type refers to a definition for a
class or structure whereas a DbType is an enumeration that typically
is more or less correlated to a provider specific enumeration like
SqlType or OracleType. That is why you can't cast or pass one as the
other; as Miha says, they are different things.

Yes, you are correct (though DbType is independent to providers).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top