在使用asp.net中的存储过程实现动态搜索时遇到问题 我有一个数据表和数据库的考勤系统我想搜索一个员工的主要控制,如他的ID和日期,设备类型......等 这是我的Default.aspx.cs代码
public partial class _Default : Page
{
static string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
this.gridData();
this.search();
}
}
//--------------------------------------------------
private void gridData()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT EventUserId, EventDate, EventTime, Eventtype, Readerid FROM V_EventLogs"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
private DataTable search()
{
DataTable sdt = new DataTable();
SqlConnection con = new SqlConnection(constr);
try
{
SqlCommand cmd = new SqlCommand("userSearch", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 300;
cmd.Parameters.AddWithValue("@EventUserId", TextBox1.Text);
cmd.Parameters.AddWithValue("@EventDateFrm", ASPxDateEdit1.Text);
cmd.Parameters.AddWithValue("@EventDateTo", ASPxDateEdit2.Text);
cmd.Parameters.AddWithValue("@Readerid", txtReader.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// passs parameter to data adapter
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(sdt);
}
catch (Exception ex) {
Response.Write(ex);
}
return sdt;
}
//---------------------------------------------------
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.gridData();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
GridView1.DataSource = search();
GridView1.DataBind();
}
}
}
这是我的Default.aspx页面:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<div class="jumbotron">
<h1>BioWeb TimeTel</h1>
<p class="lead">Time Attendance</p>
<%--<p><a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more »</a></p>--%>
</div>
<div>
User ID:
<asp:TextBox ID="TextBox1" runat="server" CssClass="form-control"></asp:TextBox>
<br />
<br />
Event Date:
<br />
<dx:ASPxDateEdit ID="ASPxDateEdit1" runat="server" DisplayFormatString="MM/dd/yyyy" EditFormatString="MM/dd/yyyy">
</dx:ASPxDateEdit>
<br />
Event Time:
<br />
<dx:ASPxDateEdit ID="ASPxDateEdit2" runat="server" DisplayFormatString="MM/dd/yyyy" EditFormatString="MM/dd/yyyy">
</dx:ASPxDateEdit>
<br />
<br />
Reader:
<asp:TextBox ID="txtReader" runat="server" CssClass="form-control" AutoCompleteType="Disabled"></asp:TextBox>
<br />
<br />
 
<br />
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" CssClass="btn btn-primary btn-lg" OnLoad="Page_Load" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" PageSize="10" UseAccessibleHeader="True" CssClass="table table-hover">
<Columns>
<asp:BoundField DataField="EventUserId" HeaderText="User ID" />
<asp:BoundField DataField="EventDate" HeaderText="Date IN" />
<asp:BoundField DataField="EventTime" HeaderText="Time IN" />
<asp:BoundField DataField="Eventtype" HeaderText="Type" />
<asp:BoundField DataField="Readerid" HeaderText="Reader" />
</Columns>
</asp:GridView>
</div>
我存储的程序是:
CREATE procedure userSearch
(
@EventUserId int = NULL,
@EventDateFrm nvarchar(10) = NULL,
@EventDateTo nvarchar(10) = NULL,
@Readerid int = NULL
)
as
select V_EventLogs.EventUserId,V_EventLogs.EventDate,V_EventLogs.EventTime,V_EventLogs.Eventtype,V_EventLogs.Readerid
from V_EventLogs
where
(@EventUserId is NULL OR EventUserId = @EventUserId )
AND
(@EventDateFrm is NULL OR Convert(varchar(10),EventDate,110) >= Convert(varchar(10),@EventDateFrm,110))
AND
(@EventDateTo is NULL OR Convert(varchar(10),EventDate,110) <= Convert(varchar(10),@EventDateTo,110))
AND
(@Readerid is NULL OR Readerid = @Readerid)
现在当我想搜索一个员工时,网格数据表仍然是空的并且没有获得任何值但是在SQL服务器上测试我的存储过程时工作正常 - 第二个问题是当我选择任何其他页面搜索实现不起作用时,分页数据表动态搜索仅适用于第一页,数据表显示所有数据库值!为什么??
答案 0 :(得分:0)
我建议您仅在填充命令后关闭连接,并且不要调用ExecuteNonQuery,如下所示:
SqlCommand cmd = new SqlCommand("userSearch", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 300;
cmd.Parameters.AddWithValue("@EventUserId", TextBox1.Text);
cmd.Parameters.AddWithValue("@EventDateFrm", ASPxDateEdit1.Text);
cmd.Parameters.AddWithValue("@EventDateTo", ASPxDateEdit2.Text);
cmd.Parameters.AddWithValue("@Readerid", txtReader.Text);
// passs parameter to data adapter
SqlDataAdapter sda = new SqlDataAdapter(cmd);
con.Open();
sda.Fill(sdt);
con.Close();
如果您希望使用DataAdapter,请执行此操作。否则,您可以直接检索数据:
SqlCommand cmd = new SqlCommand("userSearch", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 300;
cmd.Parameters.AddWithValue("@EventUserId", TextBox1.Text);
cmd.Parameters.AddWithValue("@EventDateFrm", ASPxDateEdit1.Text);
cmd.Parameters.AddWithValue("@EventDateTo", ASPxDateEdit2.Text);
cmd.Parameters.AddWithValue("@Readerid", txtReader.Text);
con.Open();
var reader = cmd.ExecuteReader();
var dtbl = new DataTable();
dtbl.Load(reader);
con.Close();
当然最好将所有内容放入try-catch中并使用&#34;使用&#34; .....: - )
答案 1 :(得分:0)
答案在你的问题中。您没有在PageIndexChanging ...
中进行任何搜索protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.gridData();
}
上面的代码只是简单地重新绑定整个数据,因为你的gridData()方法检索所有记录,设置NewPageIndex只会将你移动到那个页面。
尝试写下面的内容:
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = search();
}
这不是一个完美的解决方案。应该使用存储过程本身实现的分页;此SP应将页面索引和每页的记录数与其他搜索条件一起作为输入;您需要构建一个动态SQL以在SP中获取相应的页面结果并将其绑定回Grid。简单的Google搜索可能会为您提供其他解决方案。但是,上述变化应该符合您的要求。
答案 2 :(得分:0)
您可以创建动态搜索SQL Server存储过程
Create PROCEDURE CUSTOMER.GET_DynamicSearch ( -- Optional Filters for Dynamic Search @CustomerID INT = NULL, @CustomerName NVARCHAR(50) = NULL, @CustomerTitle NVARCHAR(50) = NULL, @locationId int = NULL, @ControlCardNumber bigint = NULL, @Formnumber int = NULL ) AS BEGIN SET NOCOUNT ON DECLARE @lCustomerID INT = NULL, @lCustomerName NVARCHAR(50) = NULL, @lCustomerTitle NVARCHAR(50) = NULL, @llocationId int = NULL, @lControlCardNumber bigint = NULL, @lFormnumber int = NULL SET @lCustomerID = @CustomerID SET @lCustomerName = LTRIM(RTRIM(@CustomerName)) SET @lCustomerTitle = LTRIM(RTRIM(@CustomerTitle)) SET @llocationId = LTRIM(RTRIM(@locationId)) SET @lControlCardNumber = @ControlCardNumber SET @lFormnumber = LTRIM(RTRIM(@Formnumber)) SELECT c.CustomerId, c.CustomerName, c.CustomerTitle, c.LocationId, c.ControlCardNumber, c.FormNumber FROM CUSTOMER.Customer c WHERE (@lCustomerID IS NULL OR CustomerId = @lCustomerID) AND (@lCustomerName IS NULL OR CustomerName LIKE '%' +@lCustomerName + '%') AND (@lCustomerTitle IS NULL OR CustomerTitle LIKE '%' + @lCustomerTitle + '%') AND (@llocationId IS NULL OR locationId = @llocationId ) AND (@lControlCardNumber IS NULL OR ControlCardNumber = @lControlCardNumber) AND (@lFormnumber IS NULL OR Formnumber = @lFormnumber) ORDER BY c.CustomerID asc END GO
执行细节
EXEC CUSTOMER.GET_DynamicSearch -- 2000 records
- 仅提供CustomerName参数:
EXEC CUSTOMER.GET_DynamicSearch @CustomerName= 'Uttam' -- 1040 records