How can I send a string as NULL to SQLServer using Dapper?

眉间皱痕 提交于 2019-12-04 23:54:46
Scen

You can choose not to send StartDate.

Example:

dynamic parameters = new {

};

if (!string.IsNullOrWhiteSpace(startDate)) 
{
   parameters.StartDate = startDate;
}

connection.Query<MyObject>("[dbo].[sp_MyStoredProcedure]"), parameters, commandType: CommandType.StoredProcedure);

Edit:

Also, your stored procedure must accept nulls. Here's an example:

CREATE PROCEDURE [ExampleProc]
   @StartDate datetime = null
AS
   Select @StartDate
Marc Gravell

Yes, dapper knows to replace reference-null with DBNull.Value whenever it sees it. Because DBNull is my arch -nemesis, and if I never see it again in my application code (library code is different) I will die a little happier.

See also: https://stackoverflow.com/a/9632050/23354

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!