L
lbolognini
Hi all,
I'm building a data mapper class where each property of the class maps
a column in the database (guess this is formally called by Fowler in
PoEAA a Data Mapper or an Active Record pattern).
Ok the problem is I have several columns in the User table which have
Foreign Key constraints and allow NULL values to be stored.
I tried several approaches for passing null values to the db but none
of them seems to work. Columns declared as bigint in SQL Server 2005
got passed a 0 instead of NULL even if I use the following C# code to
prevent it:
1st approach (this is how LLBGEN generates the code)
CreateCommand.Parameters.Add(new SqlParameter("@LangID",
SqlDbType.BigInt, 8, ParameterDirection.Input, true, 19, 0, "",
DataRowVersion.Proposed, LangID));
in this case the "true" which is the fith parameter in this overriden
constructor of the parameter still gets the property initialized at 0
even if I don't set it in my code and even if I execute the code below
in the second approach.
2nd approach
foreach (SqlParameter Parameter in CreateCommand.Parameters)
{
if (Parameter.Value == null)
{ Parameter.Value = DBNull.Value;}
}
2nd-bis approach
foreach (SqlParameter Parameter in CreateCommand.Parameters)
{
if ((Parameter.Value == null) | (Parameter.Value == 0))
{Parameter.Value = DBNull.Value;}
}
this returns the error Operator '==' cannot be applied to operands of
type 'object' and 'int'
3rd approach
Nullable Types declaring long? but gives an error and found out that
ADO.NET doesn't support Nullable types
(http://unboxedsolutions.com/sean/archive/2005/09/10/727.aspx). Not
clear though since using SqlClient ADO.NET should be bypassed.
So any clues of how to handle such problem? I'm pretty sure I'm
overlooking something since it must be a problem faced by everybody
long time ago.
BTW I'm using SqlServer 2005, ADO.NET and C#2 with the .NET framework v
2 obviously.
Thanks in advance,
Lorenzo
I'm building a data mapper class where each property of the class maps
a column in the database (guess this is formally called by Fowler in
PoEAA a Data Mapper or an Active Record pattern).
Ok the problem is I have several columns in the User table which have
Foreign Key constraints and allow NULL values to be stored.
I tried several approaches for passing null values to the db but none
of them seems to work. Columns declared as bigint in SQL Server 2005
got passed a 0 instead of NULL even if I use the following C# code to
prevent it:
1st approach (this is how LLBGEN generates the code)
CreateCommand.Parameters.Add(new SqlParameter("@LangID",
SqlDbType.BigInt, 8, ParameterDirection.Input, true, 19, 0, "",
DataRowVersion.Proposed, LangID));
in this case the "true" which is the fith parameter in this overriden
constructor of the parameter still gets the property initialized at 0
even if I don't set it in my code and even if I execute the code below
in the second approach.
2nd approach
foreach (SqlParameter Parameter in CreateCommand.Parameters)
{
if (Parameter.Value == null)
{ Parameter.Value = DBNull.Value;}
}
2nd-bis approach
foreach (SqlParameter Parameter in CreateCommand.Parameters)
{
if ((Parameter.Value == null) | (Parameter.Value == 0))
{Parameter.Value = DBNull.Value;}
}
this returns the error Operator '==' cannot be applied to operands of
type 'object' and 'int'
3rd approach
Nullable Types declaring long? but gives an error and found out that
ADO.NET doesn't support Nullable types
(http://unboxedsolutions.com/sean/archive/2005/09/10/727.aspx). Not
clear though since using SqlClient ADO.NET should be bypassed.
So any clues of how to handle such problem? I'm pretty sure I'm
overlooking something since it must be a problem faced by everybody
long time ago.
BTW I'm using SqlServer 2005, ADO.NET and C#2 with the .NET framework v
2 obviously.
Thanks in advance,
Lorenzo