在asp控件中编写一个sql语句

时间:2011-01-12 19:27:12

标签: c# .net asp.net sql

我应该如何在SqlDataSource控件中编写sql语句?

<% String inuser="john";%>
<asp:SqlDataSource ID="SqlDataSource1" 
                   runat="server" 
                   ConnectionString="..." 
                   SelectCommand="select from users where user='<%#inuser%>'??????????????" DeleteCommand="..." InsertCommand="..." 
                   UpdateCommand="...">
</asp:SqlDataSource>

1 个答案:

答案 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>