无法搜索日期(月份字)

时间:2016-03-31 00:32:46

标签: c# asp.net

我有一个搜索文本框I,然后是一个用于搜索记录的按钮。

我可以按ID和状态进行搜索。但我也希望使用文本框

搜索日期

这可能吗?

ASPNET

<div class="pull-right col-lg-11">
    <center>
       <asp:TextBox ID="txtSearch" runat="server" class="form-control" bordercolor="Black"></asp:TextBox>
  </center>
</div>
<br />
<div>
    <span class="input-group-btn">
        <asp:LinkButton ID="btnSearch" runat="server" class="btn btn"
            BackColor="CornflowerBlue" Style="color: White"
            OnClick="btnSearch_Click">
         <i class="fa fa-search"></i>
        </asp:LinkButton>
    </span>
</div>

以下是我在我的代码背后尝试的内容。

C#代码: 按钮点击

protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Text.Trim() == "")
        {
            GetPR();
        }
        else
        {
            GetPRSearch(txtSearch.Text);
        }
    }

MY GET

void GetPR()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT Receiving.ReceivingID, Receiving.POID, Receiving.FinalDeliveryDate, " +
            "Receiving.Status, Receiving.DateReceived, Receiving.ReceivedByID, Users.FirstName, Users.LastName, PO.CompanyName FROM Receiving " +
            "INNER JOIN Users ON Receiving.ReceivedByID=Users.UserID INNER JOIN PO ON Receiving.POID=PO.POID ORDER BY Receiving.DateReceived DESC";
        cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Receiving");
        lvPurchases.DataSource = ds;
        lvPurchases.DataBind();
        con.Close();
    }

void GetPRSearch(string keyword)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT Receiving.ReceivingID, Receiving.POID, Receiving.FinalDeliveryDate, " +
            "Receiving.Status, Receiving.DateReceived, Receiving.ReceivedByID, Users.FirstName, Users.LastName, PO.CompanyName FROM Receiving " +
            "INNER JOIN Users ON Receiving.ReceivedByID=Users.UserID INNER JOIN PO ON Receiving.POID=PO.POID " +
            "WHERE Receiving.ReceivingID LIKE '%" + keyword + "%' OR PO.CompanyName LIKE '%" + keyword + "%' OR Receiving.POID LIKE '%" + keyword + "%' " +
            "OR Receiving.DateReceived LIKE '%" + keyword + "%'";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Receiving");
        lvPurchases.DataSource = ds;
        lvPurchases.DataBind();
        con.Close();
    }

通过使用此无法搜索“四月” 提前谢谢你们!建议非常感谢。

2 个答案:

答案 0 :(得分:0)

如果您的数据库中的值存储为日期时间SQL将自动将字符串转换为日期时间。所以你可以这样做:

SELECT * FROM tablename
WHERE tablename.date = '04/01/2016'

如果您想选择整个月的值,则需要选择范围:

SELECT * FROM tablename
WHERE tablename.date >= '04/01/2016'
AND tablename.date <= '04/30/2016'

如果您的值存储为字符串 您需要将值转换为日期时间才能使范围起作用。这样做:

SELECT * FROM tablename
WHERE convert(datetime, tablename.date, 101) >= '04/01/2016'
AND convert(datetime, tablename.date, 101) <= '04/30/2016'

如果您让用户只选择整个月而不是一天,我会使用下拉列表而不是文本框。

答案 1 :(得分:0)

在考虑使用sql server的情况下,您可以简单地执行此操作。

"Select * from yourtable WHERE Datename(month,YOURDATEColumn) like '%'+ keyword + '%'"

这样内置函数将以当前语言检索月份名称。

您可以在此处找到有关此https://msdn.microsoft.com/es-es/library/ms174395%28v=sql.120%29.aspx

的更多信息