I am trying to pass default to SQL Server in c#.
This code passes null. Is there any way to pass default?
foreach (SqlParameter Parameter in command.Parameters)
{
if (Convert.ToString(Parameter.Value) == "")
{
Parameter.Value = DBNull.Value;
}
}
Either don't add the parameter, or add the parameter but set the value to null
( not DBNull.Value
). Both have the same effect: the parameter is not passed, therefore the default is used.
You can set the parameter default in SQL-Server (Stored Procedure...)
(@parm varchar(5) = null)
AS
Begin...
And then don't pass the parameter from C# at all.
Here is another solution of sorts -- it appears to search the stored procedure for parameter default values (literally, a pattern search for the assignment in the declaration of the variable), and return any default values that are found. So...you could call this procedure from your c# app to get all of your default values, assign them to local C# vars, and then use them when you call your other procedure.
http://www.codeproject.com/Articles/12939/Figure-Out-the-Default-Values-of-Stored-Procedure
And here is how you could find the default value on a table itself: (you might need to strip the parenthesis off the returned value)
SELECT COLUMN_DEFAULT, replace(replace(COLUMN_DEFAULT,'(',''),')','') as DefaultWithNoParenthesis
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'WHATEVER'
Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.