DB Type casting

Hi,

To be indepentend from the data provider, I wrote a DataLayer. This datalayer has a GenericParameter class which inherits IDataParameter. When I use a Sql Provider, then I need to convert the DbType to SqlDbType like this: (SqlDbType)param.DbType . But it converts the types completly wrong. E.g.: DbType.Int32 -> SqlDbType.NText and DbType.String -> SqlDbType.BigInt ! Does someone knows what I should do

Thanks, Rainer.


Answer this question

DB Type casting

  • frusciante

    DbType is an enumeration.  The first value in the enumeration is AnsiString.  SqlDbType is also an enumeration and it's first value is BigInt.  What you are doing is taking the numeric value of the param.DbType and casting it back to an enumeration.  All this will do is map their actual offsets, not their types.

    What you want is the following:

    SqlParameter param = new SqlParameter();
    param.DbType = myGenericDbParameter.DbType;

    The DbType and SqlDbType are linked, so if you set the DbType it will automatically update the SqlDbType as appropriate.

  • DB Type casting