如何在代码隐藏文件中使用 count 函数返回的值
我知道在 sql 查询中可以使用 count 函数返回受影响的行数。我需要使用 sql select 语句返回的值对隐藏文件的代码做出决定。
<asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CUSTOMERConnectionString %>"
SelectCommand=
"SELECT COUNT O.siren, O.siret, O.sap_code, CLIENT.display_name, CLIENT.departement, STATUS.status_lib_fr, PERSONNEL.dbo.EMPLOYES.display, CLIENT.client_id
FROM ORGANIZATION AS O INNER JOIN CLIENT ON O.client_id = CLIENT.client_id
WHERE (O.siren = @value) OR (O.siret = @value) OR (O.sap_code = @value) OR (O.name1 LIKE '%' + @value + '%') OR (O.name2 LIKE '%' + @value + '%') OR (O.name_other LIKE '%' + @value + '%') OR (O.name_short LIKE '%' + @value + '%') OR (O.name_long LIKE '%' + @value + '%')" >
<SelectParameters>
<asp:ControlParameter ControlID="TxtValue" Name="value" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:sqldatasource>
<asp:TextBox ID="TxtValue" runat="server" Width="164px"/>
因此,如果 select 命令返回的值为零,那么我需要在后面的代码中执行另一组操作。但如何获取这个值呢?或者有没有其他方法来确定 select stmt 是否返回任何值? 谢谢!
i know in sql query one can use count function to return the number of rows affected. I need to make a decision on the code behind file using the value returned by my sql select statement.
<asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CUSTOMERConnectionString %>"
SelectCommand=
"SELECT COUNT O.siren, O.siret, O.sap_code, CLIENT.display_name, CLIENT.departement, STATUS.status_lib_fr, PERSONNEL.dbo.EMPLOYES.display, CLIENT.client_id
FROM ORGANIZATION AS O INNER JOIN CLIENT ON O.client_id = CLIENT.client_id
WHERE (O.siren = @value) OR (O.siret = @value) OR (O.sap_code = @value) OR (O.name1 LIKE '%' + @value + '%') OR (O.name2 LIKE '%' + @value + '%') OR (O.name_other LIKE '%' + @value + '%') OR (O.name_short LIKE '%' + @value + '%') OR (O.name_long LIKE '%' + @value + '%')" >
<SelectParameters>
<asp:ControlParameter ControlID="TxtValue" Name="value" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:sqldatasource>
<asp:TextBox ID="TxtValue" runat="server" Width="164px"/>
and so if the values returned by the select command is zero then i need to perform another set of operation in the code behind. but how do one access this value? or is there any other way to determine if the select stmt returned any value or not?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接将 SQL 语句放在代码隐藏文件中呢?更好的是,执行 LINQ to SQL 查询,该查询将以
IEnumberable
或类似的形式输出结果,具体取决于您可以以各种方式使用的数据。您可以对其调用 .Count() 以查看查询返回了多少条记录,或者如果您只想测试它是否为空,则可以调用 .Any()。这会将所有数据保留在 C# 代码中,然后您可以根据需要将其发送到页面。可以在此处找到 Linq to SQL 的备忘单 http://msdn.microsoft。 com/en-us/vstudio/bb688085 可以在这里找到更详细的解释http://msdn.microsoft.com/en-us/library/bb425822.aspx。
最近,我必须在我的一个项目中实现 Linq to XML,因为我只有 SQL 经验,并且它的多功能性和易用性给我留下了深刻的印象。绝对值得一看。
Why not put the SQL statement directly in your code-behind file? And better yet, perform a LINQ to SQL query which will spit out a result in the form of
IEnumberable<string>
or something similar dependent on your data that you can play with in all kinds of ways. You can call .Count() on it to see how many records the query returned, or if you just want to test if it is empty, .Any().This keeps all the data in the C# code behind which you can then send to the page if need be. A sort of cheat sheet for Linq to SQL can be found here http://msdn.microsoft.com/en-us/vstudio/bb688085 and a more lengthy explanation of it can be found here http://msdn.microsoft.com/en-us/library/bb425822.aspx.
I recently had to implement Linq to XML in a project of mine having only had experience in SQL, and was impressed by its versatility and ease of use. Definitely worth taking a look at.
您可以从
TxtValue.Text
检索值,例如在OnPreRender
事件处理程序中。You could retrieve the value from
TxtValue.Text
, for example in theOnPreRender
event handler.