来自C#的EXEC sp,参数可能为NULL

编程入门 行业动态 更新时间:2024-10-28 14:22:58
本文介绍了来自C#的EXEC sp,参数可能为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下sp:

创建 PROCEDURE SheriffSale.usp_combinedsearch @ SaleId nvarchar ( 25 )= NULL , @城市 nvarchar ( 50 )= NULL , @ ZipCode nvarchar ( 10 )= NULL , @County nvarchar ( 25 )= NULL , @ JudgementAmountMin money = NULL , @ JudgementAmountMax money = NULL , @AssessedValueMin money = NULL , @AssessedValueMax money = NULL , @ saledatestart 日期 = NULL , @ saledateend 日期 = NULL AS SELECT s.SaleId,s.CaseNumber,s.County,s.Mapsco,s.Plaintiff,s.Defendant, s.Address,s.City,s.State,s。 ZipCode,s.JudgementAmount,s.YearOfConstruction, s.LegalDesc ription,s .AssessedValue FROM Sale s WHERE (s.City = @ City 或 @ City IS NULL ) AND (s。 ZipCode = @ ZipCode OR @ ZipCode IS NULL ) AND ( s.County = @County OR @County IS NULL ) AND (s.JudgementAmount> = @ JudgementAmountMin OR @ JudgementAmountMin IS NULL ) AND (s.JudgementAmount< = @ JudgementAmountMax OR @ JudgementAmountMax IS NULL ) AND (s .AssessedValue> = @AssessedValueMin OR @ JudgementAmountMin IS NULL ) AND (s.AssessedValue< = @ JudgementAmountMax OR @ JudgementAmountMax IS NULL ) AND (s.SaleDate> = @saledatestart 或 @ saledatestart IS NULL ) AND (s.SaleDate< = @ saledateend 或 @ saledateend IS NULL ) ORDER BY s.SaleId OPTION (RECOMPILE)

使用C#我想使用从文本框的用户输入获取的参数来执行sp,这些参数可能包含也可能不包含值。例如,我可能只有city和JudgementAmountMix和JudgementAmountMax的值。我想让查询值来自文本框中的搜索变量。 我还需要将结果返回给调用方法,以便在WPF数据网格中使用。我花了好几天在网上尝试不同的例子而没有结果,我正在进入交付的截止日期。我知道这对于使用SQL Server的任何人来说都是一个非常标准的要求,我只是已经没时间了,并且没有耐心试图找到解决方案。任何帮助将不胜感激,请提供尽可能完整的解决方案,因为我是相当新的。

解决方案

我个人会保持这种情况简单,所以我会为所有参数定义一个值。 对于每个参数,我选择用户输入(如果存在或不存在),然后传递System.DBNull.Value [ ^ ]到程序。 因此设置参数可能是这样的(如果saleId是传递给调用该过程的方法的参数。

... command.Parameters.AddWithValue( @ SaleId, saleId == null ?System.DBNull.Value:( object )saleId); 。 ..

I have the following sp:

CREATE PROCEDURE SheriffSale.usp_combinedsearch @SaleId nvarchar(25) = NULL, @City nvarchar(50) = NULL, @ZipCode nvarchar(10) = NULL, @County nvarchar(25) = NULL, @JudgementAmountMin money = NULL, @JudgementAmountMax money = NULL, @AssessedValueMin money = NULL, @AssessedValueMax money = NULL, @saledatestart Date = NULL, @saledateend Date = NULL AS SELECT s.SaleId, s.CaseNumber, s.County, s.Mapsco, s.Plaintiff, s.Defendant, s.Address, s.City, s.State, s.ZipCode, s.JudgementAmount, s.YearOfConstruction, s.LegalDescription, s.AssessedValue FROM Sale s WHERE (s.City = @City OR @City IS NULL) AND (s.ZipCode = @ZipCode OR @ZipCode IS NULL) AND (s.County = @County OR @County IS NULL) AND (s.JudgementAmount >= @JudgementAmountMin OR @JudgementAmountMin IS NULL) AND (s.JudgementAmount <= @JudgementAmountMax OR @JudgementAmountMax IS NULL) AND (s.AssessedValue >= @AssessedValueMin OR @JudgementAmountMin IS NULL) AND (s.AssessedValue <= @JudgementAmountMax OR @JudgementAmountMax IS NULL) AND (s.SaleDate >= @saledatestart OR @saledatestart IS NULL) AND (s.SaleDate <= @saledateend OR @saledateend IS NULL) ORDER BY s.SaleId OPTION (RECOMPILE)

Using C# I would like to cause the sp to execute with parameters taken from user input from textboxes which may or may not contain a value. For example, I may have a value for city and for JudgementAmountMix and JudgementAmountMax only. I would like to have the query value the search variables from the textboxes. I also need to have the result returned to the calling method for use in a WPF datagrid. I have spent days trying different examples on the web with no results and I am running into a deadline for delivery. I know that this has to be a pretty standard type of requirement for anyone working with SQL Server, I just have run out of time and run out of patience trying to find a solution. Any assistance would be appreciated and please provide as complete a solution as possible as I am fairly new at this.

解决方案

Personally I would keep the situation simple so that I would define a value for all parameters. For each parameter I'd choose either the user input if it exists or if not, then pass System.DBNull.Value[^] to the procedure. So setting the parameters could be something like (if saleId is a parameter passed to the method calling the procedure.

... command.Parameters.AddWithValue("@SaleId", saleId == null ? System.DBNull.Value : (object)saleId); ...

更多推荐

来自C#的EXEC sp,参数可能为NULL

本文发布于:2023-10-22 22:09:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能为   参数   EXEC   sp   NULL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!