C# 将空值发送到存储过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13428563/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send null value into stored procedure
提问by Subha
How to send null value for table value parameters to a stored procedure in C#?
如何将表值参数的空值发送到 C# 中的存储过程?
回答by ????
Use like this
像这样使用
command.Parameters.AddWithValue("@param", DBNull.Value);
Here is a good link on the same topic
How to pass a null variable to a SQL Stored Procedure from C#.net code
You can go through link.
Using DBNull.Value with SqlParameter without knowing sqlDbType?
这是关于同一主题的一个很好的链接
How to pass a null variable to a SQL Stored Procedure from C#.net code
您可以通过链接。
在不知道 sqlDbType 的情况下将 DBNull.Value 与 SqlParameter 一起使用?
回答by rs.
You can add null as default paramter value and dont send any value if you want it to be null
您可以添加 null 作为默认参数值,如果您希望它为 null,则不要发送任何值
ex:
前任:
create procedure sp_name(@param1 varhcra(10), @param2 varchar(10)=null)
C#
C#
command.Parameters.AddWithValue("@param1", "value1");
//dont pass any value for param2, it is considered null
回答by Chirag Soni
Use DBNull.Valueto pass NULL.
使用DBNull.Value通过NULL。
回答by Hareendra Chamara Philips
There is an little issue using DBNull.Value though. Lets say i have a bit in the store procedure named IO.
但是使用 DBNull.Value 有一个小问题。假设我在名为 IO 的存储过程中有一点。
in the store procedure
在存储过程中
   CREATE PROCEDURE [dbo].[stp_Check]
   @IO BIT
Then at the code I add the parameter as follows.
然后在代码中我添加如下参数。
   object parameter = new SqlParameter("IO", true); //using false is the same.
And then I call the store procedure
然后我调用存储过程
   _repositary.DataContext.Database.SqlQuery<CallDetail>("exec stp_Check @IO", parameter)();
Here _repository is my DataRepository class with context. Knowing that it'll execute the sp is enough for this.
这里 _repository 是我的带有上下文的 DataRepository 类。知道它将执行 sp 就足够了。
You can catch the call using sql server profiler. That will be generated as follows.
您可以使用 sql server profiler 接听电话。这将按如下方式生成。
declare @p24 int
set @p24=7
exec sp_executesql N'exec stp_Check @IO', --parameter passed
N'@IO bit', -- parameter with the type passed
@IO=1 --parameter value used when executing the sp
select @p24
The problem is this. When you create the parameter with DBNull.Value as follows.
问题是这个。当您使用 DBNull.Value 创建参数时,如下所示。
    object parameter = new SqlParameter("IO", (object)DBNull.Value);
From the sql server profiler what you can find out is
从 sql server profiler 中你能发现的是
declare @p24 int
set @p24=7
exec sp_executesql N'exec stp_Check @IO', --parameter passed
N'@IO nvarchar(4000)', -- the passes type is not boolean
@IO=NULL --parameter value is NULL
select @p24
True that this will work.But this is not what is intended. I prefer
确实这会起作用。但这不是预期的。我更喜欢
using System.Data.SqlTypes;
You can pass null as follows.
您可以按如下方式传递 null。
 object parameter = new SqlParameter("IO", System.Data.SqlTypes.SqlBoolean.Null );
Then the request is like this.
然后请求是这样的。
declare @p24 int
set @p24=7
exec sp_executesql N'exec stp_Check @IO', --parameter passed
N'@IO bit', -- the passes type is boolean
@IO=NULL --parameter value is NULL
select @p24
There are null values for all sqltypes such as int, bigint, datetime etc. Hope this help. Cheers. :)
所有 sqltypes 都有空值,例如 int、bigint、datetime 等。希望这会有所帮助。干杯。:)

