I have the following stored procedure
CREATE PROCEDURE [dbo].[usp_GetData]
@foo VARCHAR (20), @bar bit = 1
AS ...
This provides the correct result when called in SSMS.
EXEC dbo.usp_GetData @foo = 'Hellow World', @bar = 0
Although when calling within a C# application as per below
cmd.Parameters.Add(new SqlParameter("@foo", foo));
cmd.Parameters.Add(new SqlParameter("@bar", 0));
& is captured by the profiler as below.
exec dbo.usp_GetData @foo=N'Hello World',@bar=default
Does a parameter that overides the default have to be passed differently?
Use
cmd.Parameters.AddWithValue("@bar", 0)
This way you know you actually passing the value.
new SqlParameter("@bar", 0)
triggers the wrong overload - string parameterName, SqlDbType dbType
. The value ends up being not set.
You want the one that sets the value, so it's ought to be new SqlParameter("@bar", (object)0)
.
This is why AddWithValue
was introduced.
更好,更安全:
cmd.Parameters.Add("@bar", SqlDbType.NVarChar, 16, "bar");
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.