使用LIKE的SqlDataSource SelectCommand不起作用

我在SelectCommand有以下T-SQL:

 SELECT h.Business, hrl.frn FROM registration hrl INNER JOIN holder h on h.call = hrl.call WHERE (h.Business like '%' + @business + '%' and h.Business is not null) and (hrl.frn = @frn and hrl.frn is not null) 

businessfrn绑定到控制参数,它应该返回数据,即使其中一个或两个都留空,但如果我只为frn输入数据,它不会返回任何内容。 我认为我的T-SQL做得不对,我也不确定我是否正确处理了like

如果两个文本框都留空,则应返回所有数据。 如果输入frn ,但是business留空,则只应返回与该frn相关的数据。 如果输入了business ,但是frn为空,则应返回所有匹配项, like business 。 如果两者都输入,则应返回仅与frnbusiness匹配的数据。

另外,我不确定是否真的需要做and is not null

 protected void btnSearch_Click(object sender, EventArgs e) { if (txtFRN.Text == "") frn = null; if (txtBusiness.Text == "") business = null; sqlDsMaster.SelectParameters[frn].DefaultValue = frn; sqlDsMaster.SelectParameters[business].DefaultValue = business; sqlDsMaster.DataBind(); } 

当命中此行时,上面抛出“对象引用未设置为实例”:

 sqlDsMaster.SelectParameters[frn].DefaultValue = frn; 

frnbusiness是属性。


这是SearchMaster存储过程:

 CREAETE PROCEDURE SearchMaster @business nvarchar(300) = NULL, @frn nvarchar(10) = NULL AS SELECT h.Business, hrl.frn FROM registration hrl INNER JOIN holder h on h.call = hrl.call WHERE (@business IS NULL OR h.Business like '%' + @business + '%') AND (@frn IS NULL OR hrl.frn = @frn) 

这是SearchDetails存储过程:

 CREATE PROCEDURE SearchDetails @business nvarchar(300) = NULL, @frn nvarchar(10) = NULL AS SELECT hrl.call FROM registration hrl INNER JOIN holder h ON h.call = hrl.call WHERE (@business IS NULL OR h.Business LIKE '%' + @business + '%') AND (@frn IS NULL OR hrl.frn = @frn) 

以下是SearchMaster过程的SqlDataSource

 <asp:SqlDataSource ID="sqlDsDetails" runat="server" ConnectionString=" SelectCommandType="StoredProcedure" SelectCommand="SearchMaster">      

以下是SearchDetails过程的SqlDataSource

 <asp:SqlDataSource ID="sqlDsDetails" runat="server" ConnectionString=" SelectCommandType="StoredProcedure" SelectCommand="SearchDetails">      

这是绑定SqlDsMaster的按钮单击:

 protected void btnSearch_Click(object sender, EventArgs e) { sqlDsMaster.DataBind(); } 

以下是gvMaster_RowCreated ,它为详细信息创建行:

 protected void gvMaster_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { SqlDataSource ctrl = e.Row.FindControl("sqlDsDetails") as SqlDataSource; if (ctrl != null && e.Row.DataItem != null) { ctrl.SelectParameters["frn"].DefaultValue = ((DataRowView)e.Row.DataItem)["frn"].ToString(); ctrl.SelectParameters["business"].DefaultValue = ((DataRowView)e.Row.DataItem)["business"].ToString(); } } } 

如果我通过SQL Server Management Studio运行它, SearchMasterSearchDetails都可以工作,如果我输入businessfrn数据,它可以工作,但是如果我只输入一个,则不会返回任何数据。 参数设置正确吗? 另外,如果我在过程中将参数初始化为null ,是否仍然需要使用ConvertEmptyStringToNull

我会做的事情如下:

 where (@business is null or @business = '' or h.Business like '%' + @business + '%') and (@frn is null or @frn = '' or hrl.frn = @frn) 

如果在传递空搜索字符串之前使其为空,则可以跳过@yyy =”部分。

更改

“和h.Business不是空的”

“或者h.Business is null”

“而且hrl.frn不为空”

“或者hrl.frn为空”

当这些参数为null时,这将返回所有内容。

and is not null

存储过程是正确的: (@frn IS NULL OR hrl.frn = @frn) 。 如果frnnull或匹配,您想要选择任何行。 相同的模式适用于business财产。

的NullReferenceException

 sqlDsMaster.SelectParameters[frn].DefaultValue = frn; 

这会失败,因为您使用的是frn属性的值(可能为null )而不是字符串"frn"作为SelectParameters索引。 您在下一行与business而不是"business"有相同的错误。

ConvertEmptyStringToNull

我会反过来提出这个问题:如果你使用的是ConvertEmptyStringToNull ,为什么要让调用者负担相同的function呢? 除非空字符串是frnbusiness的有效值, frn我将设置ConvertEmptyStringToNull并且不关心它在调用者中。

我希望能回答你所有的问题。