我在aspx页面中有sqldatasource,在查询中我想使用一个参数,即来自会话。
以下是我的代码。请帮帮我。
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id="Here I want to use session variable"" />
答案 0 :(得分:2)
试试这个
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id=@SessionVar">
<SelectParameters>
<asp:SessionParameter Name="SessionVar" SessionField="SessionVariableName" ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
答案 1 :(得分:1)
这篇MSDN文章可以为您提供所需的信息。基本上,您将使用参数占位符“?”定义SelectCommand,然后使用SessionParameter的条目定义SelectParameters集合。
答案 2 :(得分:0)
使用参数非常简单:
<asp:SqlDataSource id="Employees" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind%>"
SelectCommand="SELECT LastName FROM Employees WHERE Title = @Title">
<SelectParameters>
<asp:ControlParameter Name="Title"
ControlID="DropDownList1"
PropertyName="SelectedValue"/>
</SelectParameters>
</asp:sqldatasource>
只需用您的变量替换参数值:
<%= Sessiom[variable_name] %>
答案 3 :(得分:0)
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=LAZY-PC;Initial Catalog=Test;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [C] WHERE ([C#] = @column1)">
<SelectParameters>
<asp:SessionParameter Name="column1" SessionField="id" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
答案 4 :(得分:0)
Johnny_D的回答非常明确,但我想指出,你可以使用SessionParameter
类:
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id= ?" />
<SelectParameters>
<asp:SessionParameter
Name="id"
SessionField="SessionVariableName"
DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>