.net – 如何将变量传递给SqlDataSource的SelectCommand?

前端之家收集整理的这篇文章主要介绍了.net – 如何将变量传递给SqlDataSource的SelectCommand?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想把代码中的变量传递给sqlDataSource的SelectCommand?

我不想使用内置的参数类型(如ControlParemeter,QueryStringParameter等)

我需要通过可否?

以下示例不工作

<asp:sqlDataSource ID="sqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:itematConnectionString %>"
    SelectCommand = "SELECT items.name,items.id FROM items INNER JOIN users_items ON items.id = users_items.id WHERE (users_items.user_id = @userId) ORDER BY users_items.date DESC">
    <SelectParameters>
    <asp:Parameter  DefaultValue="<%= userId %>" Name="userId"  DbType="Guid" />
    </SelectParameters>

解决方法

你可以在后面的代码中这样做:
sqlDataSource1.SelectParameters.Add("@userId",userId);

编辑

试试这个,而删除SelectCommand属性和SelectParameters:

<asp:sqlDataSource ID="sqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:itematConnectionString %>">

然后在后面的代码做这:

sqlDataSource1.SelectParameters.Add("userId",userId.ToString());

sqlDataSource1.SelectCommand = "SELECT items.name,items.id FROM items INNER JOIN users_items ON items.id = users_items.id WHERE (users_items.user_id = @userId) ORDER BY users_items.date DESC"

这对我工作,这也工作:

<asp:sqlDataSource ID="sqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:itematConnectionString %>"
    SelectCommand = "SELECT items.name,items.id FROM items INNER JOIN users_items ON items.id = users_items.id WHERE (users_items.user_id = @userId) ORDER BY users_items.date DESC"></asp:sqlDataSource>


sqlDataSource1.SelectParameters.Add("userid",DbType.Guid,userId.ToString());
原文链接:https://www.f2er.com/aspnet/254088.html

猜你在找的asp.Net相关文章