[#1010715] NpgsqlParameterCollection.Add and System.Data.DbType management

View Trackers | Patches | Download .csv | Monitor

2009-10-08 23:07
Submitted by:
Gildas (gildas)
Assigned to:
Nobody (None)
NpgsqlParameterCollection.Add and System.Data.DbType management

Detailed description
Concerning bug [#1010714] using 'DbType.Binary' instead of 'NpgsqlTypes.NpgsqlDbType.Bytea' for updating bytea column fails, i hesitate between two solutions in order to correct the bug (i have reproduced it).

First of all the bug comes because the user (alemarko) uses this :
> cmd.Parameters.Add("bytesData", System.Data.DbType.Binary).Value = bytes;

instead of decomposing like :
> NpgsqlParameter param = new NpgsqlParameter(":bytesData", DbType.Binary);
> param.Value = bytes;

So in this case the NpgsqlParameter constructor used is the NpgsqlParameter(String parameterName, object value) instead of this one NpgsqlParameter(String parameterName, DbType parameterType).

So the GetNativeTypeInfo will try to get the type of (DbType.Binary).GetType() That retuns DbType and not Binary and it's not known... :)

So three solutions are possible :
1 - User has to decompose your parameter additions :)
2 - Add a new Add function in the NpgsqlParameterCollection that takes a DbType instead of an object NpgsqlParameter Add(string parameterName, DbType value), so the correct NpgsqlParameter constructor will be called
3 - Add a test in the NpgsqlParameter(String parameterName, object value) constructor to test if the value (object) is a DbType, if it does use TryGetNativeTypeInfo(DbType dbType, out NpgsqlNativeTypeInfo typeInfo) by casting value, instead of use TryGetNativeTypeInfo(Type type, out NpgsqlNativeTypeInfo typeInfo)

The two patchs, for the two methods (2 and 3) are in the attached zip file


Date: 2009-12-19 15:15
Sender: Francisco Figueiredo jr.


I'm copying here what I said in the 1010714 bug report.

I added a method called AddWithValue just like SqlParameterCollection
did in order to avoid the ambiguity of this type.

Also, you should achieve the same thing by using the Add method,
or better, the AddWithValue method, without needing to specify
the dbtype. Npgsql can and should figure it out by itself.

So, you code could be just:
cmd.Parameters.Add(":field_bytea", bytes);

I just added this modification of AddWithValue and also created
a test case to verify that Npgsql indeed recognizes correctly
the dbtype when passing only the name and bytes value.

Please, let me know what do you think.

I hope it helps.
Date: 2009-12-15 14:18
Sender: Francisco Figueiredo jr.

After checking SqlParameterCollection, it seems the Add(String, Object) method is now obsolete. I think this was done because of this type of ambiguity.

I'm thinking about doing the same with Npgsql so this type of problem doesn't appear.

Attached Files:



Field Old Value Date By
File Added433: Patchs.zip2009-10-08 23:07gildas
Powered By FusionForge