ASP.NET的SqlDataSource,像的SelectCommand [英] ASP.NET SqlDataSource, like on SelectCommand
问题描述
我正在asp.net。我有一个的SqlDataSource
与查询硬$ C $的SelectCommand上的CD:
I'm working on asp.net. I Have a SqlDataSource
with a query hardcoded on selectcommand:
<asp:SqlDataSource ID="DataSource1" runat="server" CancelSelectOnNullParameter="False"
ConnectionString="<%$ ConnectionStrings:S.Properties.Settings.ConnectionString %>"
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like Case @col1_param When null Then col1 Else @col1_param End)
and ([col2] like Case @col2_param When null Then col2 Else @col2_param End)"
SelectCommandType="Text">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="col1_param" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="TextBox2" Name="col2_param" PropertyName="Text"
Type="String" />
</SelectParameters>
我要的是,如果你在一个文本框中输入的数据而已,数据将只在WHERE子句文本框的值显示根据。如果没有值被放置无论是文本框,在执行查询,如果没有的地方。
What I want is that if you enter data on one textbox only, the data will display according with that textbox value only on the where clause. And if no values are placed for neither of the textboxes, the the query executes as if there is no where.
现在这个code,会发生什么情况是,如果你把一个TextBox只显示任何数据。如果所有的文本框为空一样的。
Right now with this code,what happens is if you put on one textbox only no data is displayed. The same if all textboxes are empty.
我不希望使用SQL存储过程。
I don't want to use sql stored procedure.
我该如何解决呢?
谢谢...
推荐答案
假设它传递空当没有输入文本,否则你将需要检查空字符串
Assuming it passes null when there is no text entered, otherwise you will need to check for the empty string
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like '%@col1_param%' or @col1_param is null)
and ([col2] like '%@col2_param%' or @col2_param is null)"
这篇关于ASP.NET的SqlDataSource,像的SelectCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!