我应该如何在SqlDataSource控件中编写sql语句?
<% String inuser="john";%>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="..."
SelectCommand="select from users where user='<%#inuser%>'??????????????" DeleteCommand="..." InsertCommand="..."
UpdateCommand="...">
</asp:SqlDataSource>
答案 0 :(得分:6)
您最好在代码隐藏的加载事件处理程序中设置值:
var inuser = "john";
SqlDataSource1.SelectCommand = "select from users where user = @user";
SqlDataSource1.SelectParameters.Add("@user", inuser);
您也可以通过标记来完成:
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="..."
SelectCommand="select from users where user=@user">
<selectparameters>
<asp:Parameter name="user" DefaultValue="john"/>
</selectparameters>
</asp:SqlDataSource>