无论我的查询条件如何,GridView都会确认所有数据?

时间:2016-10-10 18:15:06

标签: c# sql asp.net gridview asp.net-web-api

当我创建我的SQLDATASOURCE查询生成器测试的条件时,我有asp.net网页,通过多文本框过滤搜索结果就像我想要的那样但是当我使用我的asp.net页面搜索数据时结果显示不考虑我的条件任何人都可以解释为什么?我的代码就像那样

  

SQL查询

SELECT        IQCRM_NUM,
              Full_name,
              Mother_Name,
              Date_of_Birth,
              Province_of_birth,
              Job
FROM    [Criminal Main Table]
WHERE   (Full_name LIKE N'%' + @Full_name + N'%') OR
        (Mother_Name LIKE N'%' + @Mother_Name + N'%') OR
        (Date_of_Birth LIKE N'%' + @Date_of_Birth + N'%')
  

我的ASP.NET代码是

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 30%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Search" />
        <br />
        <table class="style1">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1_NAM" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2_Mother" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox3_Birth" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="IQCRM_NUM"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="IQCRM_NUM" HeaderText="IQCRM_NUM" ReadOnly="True" SortExpression="IQCRM_NUM" />
                <asp:BoundField DataField="Full_name" HeaderText="Full_name" SortExpression="Full_name" />
                <asp:BoundField DataField="Mother_Name" HeaderText="Mother_Name" SortExpression="Mother_Name" />
                <asp:BoundField DataField="Date_of_Birth" HeaderText="Date_of_Birth" SortExpression="Date_of_Birth" />
                <asp:BoundField DataField="Province_of_birth" HeaderText="Province_of_birth" SortExpression="Province_of_birth" />
                <asp:BoundField DataField="Job" HeaderText="Job" SortExpression="Job" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CriminalDBBefore2003ConnectionString %>"
            SelectCommand="SELECT IQCRM_NUM, Full_name, Mother_Name, Date_of_Birth, Province_of_birth, Job FROM [Criminal Main Table] WHERE (Full_name LIKE N'%' + @Full_name + N'%') OR (Mother_Name LIKE N'%' + @Mother_Name + N'%') OR (Date_of_Birth LIKE N'%' + @Date_of_Birth + N'%')">
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1_NAM" ConvertEmptyStringToNull="False" Name="Full_name"
                    PropertyName="Text" Type="String" />
                <asp:ControlParameter ControlID="TextBox2_Mother" ConvertEmptyStringToNull="False"
                    Name="Mother_Name" PropertyName="Text" Type="String" />
                <asp:ControlParameter ControlID="TextBox3_Birth" ConvertEmptyStringToNull="False"
                    Name="Date_of_Birth" PropertyName="Text" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以使用FilterParameters代替SelectParameters并添加FilterExpression,例如此示例。

SelectCommand - 这里我们需要设置默认的SQL Query而不使用任何WHERE子句或过滤器。

FilterExpression - 这里我们需要使用占位符{0}设置SQL Query的过滤器部分。占位符{0}将替换为ControlParameter的值,即TextBox的文本

FilterParameters - 这里我们指定FilterExpression使用的控制参数

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CriminalDBBefore2003ConnectionString %>"
               SelectCommand="SELECT IQCRM_NUM, Full_name, Mother_Name, Date_of_Birth, Province_of_birth, Job FROM Criminal Main Table" FilterExpression="Full_name LIKE '{0}%' OR Mother_Name LIKE '{1}%' OR Date_of_Birth LIKE '{2}%'">
<FilterParameters>
    <asp:ControlParameter ControlID="TextBox1_NAM" ConvertEmptyStringToNull="False" Name="Full_name"
                          PropertyName="Text" Type="String"/>
    <asp:ControlParameter ControlID="TextBox2_Mother" ConvertEmptyStringToNull="False"
                          Name="Mother_Name" PropertyName="Text" Type="String"/>
    <asp:ControlParameter ControlID="TextBox3_Birth" ConvertEmptyStringToNull="False"
                          Name="Date_of_Birth" PropertyName="Text" Type="String"/>
</FilterParameters>