I'm leaving my SqlParameter
's SqlValue
unset to indicate that the parameter should be set to the default value. This is per MSDN documentation:
Use null or do not set SqlValue to use the default value for the parameter.
When I try to execute the SqlCommand
associated with the SqlParameter
, I receive a SQL error complaining that
the parameterized query '...' expects the parameter '@p0', which was not supplied.
The problem is that the generated query calls sp_executesql
without indicating that the parameter can have a default value.
Generated SQL (problematic):
exec sp_executesql N'INSERT [dbo].[WithDefault] ([Field]) VALUES (@p0);',N'@p0 int',@p0=default
Correct SQL:
exec sp_executesql N'INSERT [dbo].[WithDefault] ([Field]) VALUES (@p0);',N'@p0 int=default',@p0=default
(Notice =default
after @p0 int
)
Question:
How do I configure SqlParameter
so that it includes =default
along with the data type of the parameter?
I'd imagine something like:
param.DbType = System.Data.DbType.Int32;
param.IsDefaultable = true; // but this property doesn't exist