Here is a useful custom user type we use to deal with nullable char(1) columns containing Y/N:
Code:
using System;
using System.Data;
using NH = NHibernate;
using NNH = Nullables.NHibernate;
namespace OurCompany.DataAccess.Providers.NHibernate.UserTypes
{
/// <summary>
/// A NHibernate <see cref="NH::Type.IType"/> for a <see cref="System.Nullable<Boolean>"/>
/// mapped to a CHAR(1) database column with values 'Y', 'N' or NULL.
/// </summary>
public class NullableYesNoType : NNH.NullableTypesType
{
public NullableYesNoType() : base(new NH.SqlTypes.StringFixedLengthSqlType(1))
{
}
public override object NullValue
{
get {return null;}
}
public override System.Type ReturnedClass
{
get {return typeof(bool?);}
}
public override object Get(IDataReader rs, int index)
{
object value = rs[index];
if (value == DBNull.Value)
{
return null;
}
else
{
return (Convert.ToChar(value) == 'Y');
}
}
public override void Set(IDbCommand cmd, object value, int index)
{
IDataParameter parameter = (IDataParameter) cmd.Parameters[index];
bool? nullableValue = (bool?) value;
if (nullableValue.HasValue)
{
parameter.Value = (nullableValue.Value ? 'Y' : 'N');
}
else
{
parameter.Value = DBNull.Value;
}
}
public override object FromStringValue(string xml)
{
if (xml == null || xml.TrimEnd().Length == 0)
{
return null;
}
else
{
return (xml[0] == 'Y');
}
}
}
}