c# nullable types as parameters

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
bradk
Posts: 34
Joined: Thu 20 Apr 2006 01:42

c# nullable types as parameters

Post by bradk » Tue 19 Jun 2007 14:34

Does the Enterprise Library Data Access Block provider support c# nullable types as parameters to pl/pgsql functions ? For example, can I add an input parameter of type int? vs. just int. Same question for OUT params in PL/PGSQL.

thanks.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Wed 20 Jun 2007 08:09

This is a hypothetical question. Could you provide us with the code snippet where you think this would be necessary?

bradk
Posts: 34
Joined: Thu 20 Apr 2006 01:42

Post by bradk » Wed 20 Jun 2007 14:21

Concretely, can the _retval and _hashkey parameters below be passed in as c# int? type, and have null values, assuming the db columns are nullable.

Code: Select all

public bool add(string config, string _subscriptionid, string _topicid, string _notifyaddress, System.DateTime _expiration, string _ownerdata, string _notifychannel, bool _hasexpiration)
{
	try
	{
		string hashSource = "eventpubsub:topic:" + _topicid.ToString();
		Database db = CreateHashedDatabase(config, hashSource);
		string sqlCommand = "subscription_add";
		DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
		db.AddInParameter(dbCommand, "_hashkey", DbType.Int32, _hashkey);
		db.AddInParameter(dbCommand, "_subscriptionid", DbType.String, _subscriptionid);
		db.AddInParameter(dbCommand, "_topicid", DbType.String, _topicid);
		db.AddInParameter(dbCommand, "_notifyaddress", DbType.String, _notifyaddress);
		db.AddInParameter(dbCommand, "_expiration", DbType.DateTime, _expiration);
		db.AddInParameter(dbCommand, "_ownerdata", DbType.String, _ownerdata);
		db.AddInParameter(dbCommand, "_notifychannel", DbType.String, _notifychannel);
		db.AddInParameter(dbCommand, "_hasexpiration", DbType.Boolean, _hasexpiration);
		db.AddOutParameter(dbCommand, "_retval", DbType.Boolean, 1);
		db.ExecuteNonQuery(dbCommand);
		return (bool)db.GetParameterValue(dbCommand, "_retval");
	}
	catch (Exception ex)
	{
		Logger.Error(11000, ex.ToString());
		throw(ex);
	}
}

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Thu 21 Jun 2007 08:56

Yes, they can be. You would have to add a function to unwrap nullable type. Take a look at the following passage:

Code: Select all

public static object UnwrapNullable(Nullable val) {

  if (!val.HasValue)
    return null;
  return val.value;
}
...
int? _hashkey;
...
db.AddInParameter(dbCommand, "_hashkey", DbType.Int32, UnwrapNullable(_hashkey)); 

Post Reply