[英]Optional column value in SqlDataSource
I have this SqlDataSource
: 我有这个SqlDataSource
:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:OracleXexdb %>"
ProviderName="<%$ ConnectionStrings:OracleXexdb.ProviderName %>"
SelectCommand="select col1, col2, col3 from table where col1 < 2000 and (col2 = :col2 OR :col2 is null)"
OnSelecting="SqlDataSource1_Selecting"
>
<SelectParameters>
<asp:ControlParameter ControlID="codagent" Name="col2" PropertyName="Text" Type="String" ConvertEmptyStringToNull="true" DefaultValue=""/>
</SelectParameters>
</asp:SqlDataSource>
codagent
is an <asp:TextBox>
and the user can put a value or nothing (""), if the user leaves the TextBox with nothing, the SqlDataSource
retrieves no values. codagent
是一个<asp:TextBox>
,用户可以输入值或不输入任何值(“”),如果用户不带任何内容离开TextBox,则SqlDataSource
检索任何值。 My goal is allow user to get all the col2 values, without filter 我的目标是允许用户无需过滤即可获取所有col2值
Am I missing something? 我想念什么吗?
Try changing your SQL to something like this 尝试将您的SQL更改为这样的内容
SelectCommand="select col1, col2, col3 from table where col1 < 2000 and (col2 = :col2 OR (:col2 is null AND 1=1)"
I'm not really sure if such expressions are possible in Oracle since I don't have any experience with it but this is the logic that is used to accomplish the same thing in SQL Server. 我不太确定在Oracle中是否可以使用这样的表达式,因为我对此没有任何经验,但这是用于在SQL Server中完成相同操作的逻辑。
Since you are already using the OnSelecting
event of SqlDataSource
, use this event to modify your Select
command. 由于您已经在使用SqlDataSource
的OnSelecting
事件,请使用此事件来修改Select
命令。
Also, since you want the user to retrieve all col2
values, in actuality, all such columns with col1<2000
will only be retrieved. 另外,由于您希望用户检索所有col2
值,因此实际上只会检索col1<2000
所有此类列。 [ Means, there can be col2
values corresponding to which col1> 2000, so such col2
will not be displayed at all ] [表示可能有col2
值对应于哪个col1> 2000,因此完全不会显示此col2
]
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
if(string.IsNullOrEmpty(codagent.Text))
{
e.Command="select col1, col2, col3 from table where col1 < 2000";
}
}
I found the way to do this: 我找到了执行此操作的方法:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:OracleXexdb %>"
ProviderName="<%$ ConnectionStrings:OracleXexdb.ProviderName %>"
SelectCommand="select col1, col2, col3 from table where col1 < 2000 and (col2 = :col2 OR (:col2 is null))"
>
<SelectParameters>
<asp:ControlParameter ControlID="codagent" Name="col2" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" DefaultValue=""/>
</SelectParameters>
</asp:SqlDataSource>
This worked fine 这很好
why don't you use subquery. 你为什么不使用子查询。 following code is giving col2, col3 whether col1 is empty or not. 以下代码为col2,col3提供了col1是否为空。
select (select col1 from table1 where col1 ='') as col1, col2, col3 from table1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.