如何将此过滤器表达式更改为参数化查询?

Posted

技术标签:

【中文标题】如何将此过滤器表达式更改为参数化查询?【英文标题】:How do I change this filter expression into a paramaterized query? 【发布时间】:2013-07-22 18:09:16 【问题描述】:

大家早上好, 如何将此过滤器表达式转换为参数化查询但仍显示在网格视图中?现在,如果我搜索 %^&%* 它会崩溃。

C#:

public static bool IsDate(Object obj)
        
            string strDate = obj.ToString();
            try
            
                DateTime dt = DateTime.Parse(strDate);
                if (dt != DateTime.MinValue && dt != DateTime.MaxValue)
                    return true;
                return false;
            
            catch
            
                return false;
            
        

 protected void BtnWinnersSearch_Click(object sender, EventArgs e)
        

            string searchText = txtWinnersSearch.Text.Replace("'", "''").Trim();
            bool isDate = IsDate(searchText);

            GridViewWinners.Visible = true;

            if (isDate == true)
            
                SqlDataSource4.FilterExpression = "dob" + " ='" + Convert.ToDateTime(searchText).ToString("yyyy-MM-dd") + "'";
            
            else
            
                SqlDataSource4.FilterExpression = "nickname like '%" + searchText + "%' or username like '%" + searchText +
                    "%' or clubnumber like '%" + searchText + "%' or firstname like '%" +
                    searchText + "%' or lastname like '%" + searchText +
                    "%' or email like '%" + searchText + "%'";
            
        

平均售价:

<asp:Panel ID="pnlWinners" DefaultButton="BtnWinnersSearch" runat="server" Visible="False">
        <table>
        <tr>
            <td align="center"><b>MANAGE WINNERS</b> - 
            <asp:Button ID="BtnWinnerAdd" runat="server" Text="Add" 
                    onclick="BtnWinnerAdd_Click" /> | 
                <asp:TextBox ID="txtWinnersSearch" runat="server" MaxLength="220"></asp:TextBox>
                    <asp:Button ID="BtnWinnersSearch"
                    runat="server" Text="Search" onclick="BtnWinnersSearch_Click"  />
            </td>
        </tr>
        <tr>
        <td>
                <asp:GridView ID="GridViewWinners" runat="server" AutoGenerateColumns="False" DataKeyNames="PlayerID"
                DataSourceID="SqlDataSource4" 
                EmptyDataText="There are no winners to display." CellPadding="4"
                AllowSorting="True" 
                onsorting="GridViewWinners_Sorting" 
                ForeColor="#333333" 
                Visible="False"
                onselectedindexchanged="GridViewWinners_SelectedIndexChanged" 
                    FooterStyle-Wrap="False" HeaderStyle-Wrap="False" PagerStyle-Wrap="False" 
                    RowStyle-Wrap="False" SelectedRowStyle-Wrap="False" Font-Overline="False" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" HorizontalAlign="Center" 
                        VerticalAlign="Middle" Wrap="False" />
                <Columns>
                    <asp:CommandField ShowSelectButton="True"/>
                    <asp:BoundField DataField="PlayerID" HeaderText="PlayerID" InsertVisible="False" 
                        ReadOnly="True" SortExpression="PlayerID" Visible="False" />
                        <asp:BoundField DataField="Nickname" HeaderText="Nickname" 
                        SortExpression="Nickname" ItemStyle-Wrap="False">
                    <ItemStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                        SortExpression="LastName" ItemStyle-Wrap="False">
                    <ItemStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                        SortExpression="FirstName" />
                    <asp:BoundField DataField="UserName" HeaderText="UserName" 
                        SortExpression="UserName" />
                    <asp:BoundField DataField="ClubNumber" HeaderText="Account Number" 
                        SortExpression="ClubNumber" />
                    <asp:BoundField DataField="Email" HeaderText="Email" 
                        SortExpression="Email" />
                    <asp:BoundField DataField="DOB" HeaderText="D.O.B." 
                        DataFormatString="0:d" SortExpression="DOB" />
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" 
                        HorizontalAlign="Center" VerticalAlign="Middle" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" 
                        HorizontalAlign="Center" VerticalAlign="Middle" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
            </td>
            </tr>
            </table>
            </asp:Panel>
             <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                ConnectionString="****" 
                ProviderName="***" 
                SelectCommand="SELECT [PlayerID], [Nickname], [UserName], [ClubNumber], [FirstName], [Email], [LastName], [DOB] FROM [Players] order by lastname ASC">
            </asp:SqlDataSource>
</asp:Content>

错误:

Like 运算符出错:字符串模式 '%@#%$%' 无效。

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.Data.EvaluateException:Like 中的错误 运算符:字符串模式 '%@#%$%' 无效。

来源错误:

在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。

堆栈跟踪:

[EvaluateException:Like 运算符中的错误:字符串模式 '%@#%$%' 无效。] System.Data.LikeNode.AnalyzePattern(String 拍)+1251846 System.Data.LikeNode.Eval(DataRow 行,DataRowVersion 版本)+345 System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode 左,ExpressionNode 右,DataRow 行,DataRowVersion 版本,Int32[] 记录号)+13013 System.Data.BinaryNode.Eval(DataRow 行,DataRowVersion 版本) +29 System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode 右,DataRow 行,DataRowVersion 版本,Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow 行, DataRowVersion 版本)+29 System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode 右,DataRow 行,DataRowVersion 版本,Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow 行, DataRowVersion 版本)+29 System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode 右,DataRow 行,DataRowVersion 版本,Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow 行, DataRowVersion 版本)+29 System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode 右,DataRow 行,DataRowVersion 版本,Int32[] recordNos) +13013 System.Data.BinaryNode.Eval(DataRow 行, DataRowVersion 版本)+29 System.Data.DataExpression.Invoke(DataRow 行,DataRowVersion 版本) +151 System.Data.Index.AcceptRecord(Int32 record, IFilter filter) +103 System.Data.Index.InitRecords(IFilter filter) +303 System.Data.Index..ctor(DataTable table, Int32[] ndexDesc, IndexField[] indexFields,Comparison`1比较,DataViewRowState 记录状态,IFilter 行过滤器)+491 System.Data.DataTable.GetIndex(IndexField[] indexDesc, DataViewRowState 记录状态,IFilter 行过滤器)+228 System.Data.DataView.UpdateIndex(布尔力,布尔fireEvent) +165 System.Data.DataView.UpdateIndex(布尔力) +12 System.Data.DataView.SetIndex2(String newSort, DataViewRowState newRowStates, IFilter newRowFilter, Boolean fireEvent) +113 System.Data.DataView.SetIndex(字符串 newSort,DataViewRowState newRowStates, IFilter newRowFilter) +17 System.Data.DataView.set_RowFilter(字符串值) +160 System.Web.UI.WebControls.FilteredDataSetHelper.CreateFilteredDataView(数据表 表、字符串 sortExpression、字符串 filterExpression、IDictionary 过滤器参数)+396 System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments 论据)+2012 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback 回调)+21 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74 System.Web.UI.WebControls.GridView.DataBind() +4 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls() +102 System.Web.UI.Control.PreRenderRecursiveInternal() +42 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +2496

真诚地, 克里斯托弗·维塔利斯·彼得森

【问题讨论】:

【参考方案1】:

您是否尝试过像 '%' + @SearchText+ '%' 那样参数化 searchText?

【讨论】:

早上好,Ispep Aloc,不,我没有。你会如何建议我这样做呢?感谢您迄今为止的帮助。此致,克里斯托弗·维塔利斯·彼得森 SelectCommand="SELECT [PlayerID], [Nickname], [UserName], [ClubNumber], [FirstName], [Email], [LastName], [DOB] FROM [Players] where Nickname like '%'+@Nickname+'%' order by lastname ASC"> asp:SqlDataSource>【参考方案2】:
 SelectCommand="SELECT [PlayerID], [Nickname], [UserName], [ClubNumber], [FirstName], [Email], [LastName], [DOB] FROM [Players] where Nickname like '%'+@Nickname+'%' order by lastname ASC">
                <SelectParameters>
                     <asp:ControlParameter ControlID="txtWinnersSearch" Name="Nickname" PropertyName="Text" Type="String"/>
                </SelectParameters>
            </asp:SqlDataSource>

【讨论】:

以上是关于如何将此过滤器表达式更改为参数化查询?的主要内容,如果未能解决你的问题,请参考以下文章

如何将此正则表达式替换从替换单个单词更改为替换单词数组?

执行参数化 N1QL 查询时,Couchbase 不使用带过滤器的索引

如何避免从 C# 构建的 Sql Server 2005 参数化查询变慢

使用route-helper将查询参数添加到现有参数

可以使用.upper()检入django rest查询过滤器

如何在 Django 管理自定义列中获取请求参数?