为什么SQL命令没有执行

时间:2014-05-12 19:52:27

标签: c# sql asprepeater

我在C#代码隐藏页面中有以下代码,可以按下按钮执行:

String cString;
    SqlConnection Conn;
    protected void Page_Load(object sender, EventArgs e)
    {

    cString = "Data Source=server;Initial Catalog=database;User Id=user12;Password=pass12;";
    }

    protected void btnGo_Click(object sender, EventArgs e) {
        Conn = new SqlConnection(cString);
        Conn.Open();
        string sqlCode = 
        "DECLARE @strLocation varchar(200) SET @strLocation = 'Ridge Hill' SELECT [content_title] AS [Physician Name], CAST([content_html] AS XML).query('/root/Physicians/picture/img') AS [Image], CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office1/a') AS [Office1], CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office2/a') AS [Office2], CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office3/a') AS [Office3], CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office4/a') AS [Office4] FROM [datbase].[dbo].[content] WHERE    [folder_id] = '188' AND (content_html LIKE '%'+@strLocation+'%') ORDER BY [content_title]";
        /* EXECUTE AND DISPLAY THE DATA IN THE ASP PAGE */


        using(SqlCommand command = new SqlCommand(sqlCode, Conn)) {
            command.CommandType = CommandType.Text;

            using (SqlDataReader reader = command.ExecuteReader()) {
                rptContent.DataSource = reader;
                rptContent.DataBind();
            }
        }
    }

我的ASPX页面中的以下代码:

<select ID="slcLocation" runat="server" ClientIDMode="Static">
    <option value="noSelect" DISABLED SELECTED>-= Select a Location =-</option>
    <option value="all">All Location</option>
    <option value="Westchester">Westchester Avenue</option>
    <option value="Theall Road">Theall Road</option>
    <option value="Davis Avenue">Davis Avenue</option>
    <option value="Market Street">Market Street</option>
    <option value="Ridge">Ridge</option>
    <option value="Overhill Road">Overhill Road</option>
    <option value="Westchester Avenue">Westchester Avenue</option>
</select>
<input type=button ID="btnGo" ClientIDMode="Static" value="Go" />
<br />
<div style="padding-left: 45px;">
    <asp:Repeater runat="server" ID="rptContent">
        <HeaderTemplate>
            <table border="0" ID="tblInfo" style="display: none;" ClientIDMode="Static">
                <tr>
                    <td>Physician Name</td>
                    <td></td>
                    <td>Office1</td>
                    <td>Office2</td>
                    <td>Office3</td>
                    <td>Office4</td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%# Eval("Physician Name").ToString() %></td>
                <td><%# Eval("Image").ToString() %></td>
                <td><%# Eval("Office1").ToString() %></td>
                <td><%# Eval("Office2").ToString() %></td>
                <td><%# Eval("Office3").ToString() %></td>
                <td><%# Eval("Office4").ToString() %></td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
</div>

两个问题:

  • 如何更改此查询行:SET @strLocation = 'Ridge Hill'以使用下拉框中的VALUE?
  • 当我点击GO按钮时,没有任何反应:/我该如何解决?

1 个答案:

答案 0 :(得分:2)

您需要将下拉列表更改为ASP:DropDownList,其中包含列表项。

然后你用'“+ slcLocation.SelectedItem.Value(或Text)+”''替换'RIDGE HILL'。