我的系统中有多个文本框和一个下拉列表,我使用“查询”构建器从数据库(SQL服务器)中的多个表中选择数据,并在网格视图中显示它们
建议ID :(文本框) 员工ID :(文本框) 状态:(下拉列表) 员工姓名:(文本框)
..............................
我希望通过输入一个字段或多个字段来搜索结果。 问题是,当我在互联网上浏览以测试查询是否有效时,我发现只有输入所有字段才能起作用。
这是查询构建器(Visual Studio 2010)中生成的查询
SELECT
SuggestionDetail.SuggestionDescription, SuggestionDetail.SuggestionID,
SuggestionDetail.DateOfSubmission, Employee.EmployeeID, Employee.City,
Employee.EmployeeName, Category.CategoryName, SuggestionStatusDetail.DateOfLastUpdate,
SuggestionStatus.StatusName
FROM
Employee
INNER JOIN
SuggestionDetail
ON
Employee.EmployeeID = SuggestionDetail.EmployeeID
INNER JOIN
Category
ON
SuggestionDetail.CategoryID = Category.CategoryID
INNER JOIN
SuggestionStatusDetail
ON
SuggestionDetail.SuggestionID = SuggestionStatusDetail.SuggestionID
INNER JOIN
SuggestionStatus
ON
SuggestionStatusDetail.StatusID = SuggestionStatus.StatusID
WHERE
(Employee.EmployeeName LIKE N'%' + @empname + N'%')
OR
(Employee.EmployeeID LIKE '%' + @empid + '%')
OR
(SuggestionDetail.SuggestionID LIKE '%' + @txtDocID + '%')
OR
(SuggestionStatus.StatusName LIKE N'%' + @StatusName + N'%')
这是HTML中生成的代码:
<asp:GridView ID="GridView1" runat="server" BackColor="White" class="GridView"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
EnableModelValidation="True" ForeColor="Black" GridLines="Vertical"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource2">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="SuggestionDescription" HeaderText="SuggestionDescription"
SortExpression="SuggestionDescription" />
<asp:BoundField DataField="SuugestionID" HeaderText="SuggestionID"
InsertVisible="False" ReadOnly="True" SortExpression="SuggestionID" />
<asp:BoundField DataField="DateOfSubmission" HeaderText="DateOfSubmission"
SortExpression="DateOfSubmission" />
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName"
SortExpression="EmployeeName" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
SortExpression="CategoryName" />
<asp:BoundField DataField="DateOfLastUpdate" HeaderText="DateOfLastUpdate"
SortExpression="DateOfLastUpdate" />
<asp:BoundField DataField="StatusName" HeaderText="StatusName"
SortExpression="StatusName" />
</Columns>
任何帮助请,我仍然是视觉工作室的新人。
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT SuggestionDetail.SuggestionDescription, SuggestionDetail.SuggestionID, SuggestionDetail.DateOfSubmission, Employee.EmployeeID, Employee.City, Employee.EmployeeName, Category.CategoryName, SuggestionStatusDetail.DateOfLastUpdate, SuggestionStatus.StatusName FROM Employee INNER JOIN SuggestionDetail ON Employee.EmployeeID = SuggestionDetail.EmployeeID INNER JOIN Category ON SuggestionDetail.CategoryID = Category.CategoryID INNER JOIN SuggestionStatusDetail ON SuggestionDetail.SuggestionID = SuggestionStatusDetail.SuggestionID INNER JOIN SuggestionStatus ON SuggestionStatusDetail.StatusID = SuggestionStatus.StatusID WHERE (Employee.EmployeeName LIKE N'%' + @empname + N'%') OR (Employee.EmployeeID LIKE '%' + @empid + '%') OR (SuggestionDetail.SuggestionID LIKE '%' + @txtDocID + '%') OR (SuggestionStatus.StatusName LIKE N'%' + @StatusName + N'%')">
<SelectParameters>
<asp:ControlParameter ControlID="txtStaffName" Name="empname"
PropertyName="Text" />
<asp:ControlParameter ControlID="txtStaffID" Name="empid" PropertyName="Text" />
<asp:ControlParameter ControlID="txtDocID" Name="txtDocID"
PropertyName="Text" />
<asp:ControlParameter ControlID="ddStatus" Name="StatusName"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
答案 0 :(得分:0)
如果连接和其他东西工作正常,那么看我在where子句中更改了一些字段。当所有参数都为null / blank(如可为空)并且根据任何一个参数返回过滤结果时,返回整个结果为null / blank。
CREATE/ALTER STOREDPROCEDURE SP_NAME
@empname varchar(50) = NULL,
@empid int = NULL,
@txtDocID int = NULL,
@StatusName varchar(50) = NULL
AS
BEGINE
SELECT
SuggestionDetail.SuggestionDescription, SuggestionDetail.SuggestionID,
SuggestionDetail.DateOfSubmission, Employee.EmployeeID, Employee.City,
Employee.EmployeeName, Category.CategoryName, SuggestionStatusDetail.DateOfLastUpdate,
SuggestionStatus.StatusName
FROM
Employee
INNER JOIN
SuggestionDetail
ON
Employee.EmployeeID = SuggestionDetail.EmployeeID
INNER JOIN
Category
ON
SuggestionDetail.CategoryID = Category.CategoryID
INNER JOIN
SuggestionStatusDetail
ON
SuggestionDetail.SuggestionID = SuggestionStatusDetail.SuggestionID
INNER JOIN
SuggestionStatus
ON
SuggestionStatusDetail.StatusID = SuggestionStatus.StatusID
WHERE
(Employee.EmployeeName LIKE N'%' + @empname + N'%' OR @empname IS NULL)
OR
(Employee.EmployeeID LIKE '%' + @empid + '%' OR @empid IS NULL)
OR
(SuggestionDetail.SuggestionID LIKE '%' + @txtDocID + '%' OR @txtDocID IS NULL)
OR
(SuggestionStatus.StatusName LIKE N'%' + @StatusName + N'%' OR @StatusName IS NULL)
END