如何从数据库中搜索asp.net中两个日期之间的数据

时间:2016-11-26 08:48:37

标签: c# asp.net sql-server

我有一个问题:两天前,我在引入协调历史的历史上遇到了一个问题我已经解散了。但是现在我想通过在图像中键入代码来查看我已经附加的两个时间段,请按下按钮搜索从数据库中获取所有数据

enter image description here

DataTable dt = PFUCommonCLass.Common.GetDataTableModify
                     (@"SELECT * FROM Tbl_Invoice
                        WHERE Fld_Date BETWEEN " + txtStartDate.Text + "And" + txtEndDate.Text + "");
                     )
GridView1.DataBind();

2 个答案:

答案 0 :(得分:1)

编写此查询

SELECT * FROM Tbl_Invoice
                        WHERE Fld_Date BETWEEN @tdate1 and @tdate2;

cmd.parameter.Addwithvalue("@tdate1",txtstartdate.text);
cmd.parameter.Addwithvalue("@tdate12",txtenddate.text);

答案 1 :(得分:0)

请观看此视频。这解释了。您可以使用日期选择器 示例代码:

string query = @"select top 1 OrderNumber 
                from tblOrderMaster 
                where OrderedDate BETWEEN @startDate AND @endDate";
using(SqlConnection conn = new SqlConnection("connectionString here"))
{
    using(SqlCommand cmd = new SqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = query;
        cmd.Parameters.AddWithValue("@startDate", txtfromcal.Text);
        cmd.Parameters.AddWithValue("@endDate", txttocal.Text);
        try
        {
            conn.Open();
            // other codes
            // to fetch the record
        }
        catch(SqlException e)
        {
            // do something with 
            // e.ToString()
        }
    }
}