SQL命令没有执行的原因

本文关键字:执行 命令 SQL | 更新日期: 2023-09-27 18:02:53

我有以下代码在我的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'使用下拉框中的值?
  • 当我点击GO按钮时,什么都没有发生:/我该如何修复它?

SQL命令没有执行的原因

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

然后你将'RIDGE HILL'替换为' ' + slclocation . selecteitem . value(或Text) + ' '…"