我正在研究asp.net。我有一个SqlDataSource
在 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 子句上根据该文本框值显示。如果没有为两个文本框放置任何值,则查询会像没有位置一样执行。
现在使用此代码,如果您将一个文本框放在一个文本框上,则不会显示任何数据。如果所有文本框都为空,则相同。
我不想使用 sql 存储过程。
我该如何解决这个问题?
谢谢...