I'm trying to create a stored procedure that would take 4 paramters. These 4 paramters dictate how the returned table will look like.
我要创建一个存储过程,它需要4个参数。这4个参数决定返回的表是什么样子。
@C_ID
parameter is always numeric. @U_ID
and @P_ID
values can contain a valid numeric value, or can be (or should be) passed as NULL
so WHERE
conditions either execute or not.
@C_ID参数总是数值型的。@U_ID和@P_ID值可以包含一个有效的数值,或者可以(或应该)作为NULL传递,以便执行或不执行条件。
ALTER PROCEDURE [dbo].[GetData]
@C_ID integer,
@U_ID integer = Null,
@P_ID integer = Null,
@SortIndex integer = 1
AS
BEGIN
SELECT
ID, P_ID, U_Name, P_Name,
FORMAT(Date_When, 'dd/MM/yyyy hh:mm tt')
FROM
SomeTable
WHERE
C_ID = @C_ID
AND (@P_ID IS NULL OR P_ID = @P_ID)
AND (@U_ID IS NULL OR U_ID = @U_ID)
ORDER BY
CASE WHEN @SortIndex = 1 THEN -ID
ELSE ID
END ASC
END
On SQL Server 2014 the following executions work fine without any errors:
在SQL Server 2014上,以下的执行工作正常,没有任何错误:
exec GetData '15', null, null, '1';
exec GetData '15', '1', null, '1';
exec GetData '15', null, '1', '1';
exec GetData '15', '1', '1', '1';
...
However on C# side the following code fails to execute:
但是在c#端,以下代码没有执行:
int? SomeValue = null;
Adapter = new SqlDataAdapter("exec GetData '15'," + SomeValue + ",null,'1';", Connection);
Adapter.Fill(Data);
which gives me an error
这会给我一个错误
Incorrect syntax near ','.
不正确的语法”、“附近。
If I change the SomeValue
variable to 1, it works perfectly fine.
如果我将SomeValue变量更改为1,它会很好地工作。
DBNull.Value
and simply leaving the parameter as ' ' do not work either.
DBNull。值并简单地将参数保留为' '也不起作用。
So the question would be: how would I (if it is possible) be able to pass nullable integer to SQL given that it can be both null and a valid number?
所以问题是:如果可能的话,我如何将可空整数传递给SQL,因为SQL既可以是null,也可以是一个有效的数字?
2 个解决方案
#1
4
Do something along those lines:
做一些类似的事情:
Adapter = new SqlDataAdapter();
//con is an open SQLConnection
var cmd = new SQLCommand("GetData", con);
cmd.Parameters.Add(new SqlParameter("@C_ID", 15));
cmd.Parameters.Add(new SqlParameter("@U_ID", SomeValue ?? DBNull.Value));
cmd.Parameters.Add(new SqlParameter("@P_ID", SomeValue ?? DBNull.Value));
cmd.Parameters.Add(new SqlParameter("@SortIndex", 1));
cmd.CommandType = CommandType.StoredProcedure;
Adapter.SelectCommand = cmd;
Adapter.Fill(Data);
The ??
is called the null-coalescing operator and checks if SomeValue
is null. If so, the value behind the ?? is used - in your case DBNull.Value
.
的? ?被称为null-coalescing运算符,检查SomeValue是否为null。如果是,那么?在您的案例DBNull.Value中。
And as GSerg already pointed out, always keep Little Bobby Tables in the back of your head.
正如GSerg已经指出的,永远在你的后脑勺上放一个小博比表。
Edit: Null-coalescing reference
编辑:空合并参考
#2
1
Did you tried something like below?
你试过以下的方法吗?
SomeValue ?? "null"
#1
4
Do something along those lines:
做一些类似的事情:
Adapter = new SqlDataAdapter();
//con is an open SQLConnection
var cmd = new SQLCommand("GetData", con);
cmd.Parameters.Add(new SqlParameter("@C_ID", 15));
cmd.Parameters.Add(new SqlParameter("@U_ID", SomeValue ?? DBNull.Value));
cmd.Parameters.Add(new SqlParameter("@P_ID", SomeValue ?? DBNull.Value));
cmd.Parameters.Add(new SqlParameter("@SortIndex", 1));
cmd.CommandType = CommandType.StoredProcedure;
Adapter.SelectCommand = cmd;
Adapter.Fill(Data);
The ??
is called the null-coalescing operator and checks if SomeValue
is null. If so, the value behind the ?? is used - in your case DBNull.Value
.
的? ?被称为null-coalescing运算符,检查SomeValue是否为null。如果是,那么?在您的案例DBNull.Value中。
And as GSerg already pointed out, always keep Little Bobby Tables in the back of your head.
正如GSerg已经指出的,永远在你的后脑勺上放一个小博比表。
Edit: Null-coalescing reference
编辑:空合并参考
#2
1
Did you tried something like below?
你试过以下的方法吗?
SomeValue ?? "null"