在asp.net中显示两个日期之间的日期

时间:2015-12-15 12:31:38

标签: c# sql asp.net sql-server date

我的aspx中有两个日历,我想在这些日历的选定日期之间显示记录。我的TeklifTarih' database属性是日期类型属性。 这是我的aspx:

<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar><br />
<asp:Calendar ID="Calendar2" runat="server"></asp:Calendar><br/>
<asp:Button ID="btnClendar" runat="server" Text="İstatistikleri Filtrele" OnClick="btnClendar_Click"/>

我的onclick方法:

protected void btnClendar_Click(object sender, EventArgs e)
    {
        string baslangicTarihi = Calendar1.SelectedDate.ToString();
        string bitisTarihi = Calendar2.SelectedDate.ToString();
        EntityDataSourcePersonel.CommandText =
            "SELECT COUNT(TeklifTable.TeklifHazirlayan) AS Basari, EmployeeTable.Name, EmployeeTable.Surname, SUM(TeklifTable.TeklifTutar) AS ToplamSatis FROM EmployeeTable JOIN TeklifTable ON TeklifTable.TeklifHazirlayan = EmployeeTable.EmployeeId WHERE TeklifTable.TeklifTarih >= " + baslangicTarihi + " AND TeklifTable.TeklifTarih <= " + bitisTarihi + " GROUP BY EmployeeTable.Name,EmployeeTable.Surname";
    }

我需要使用此命令文本显示数据,当我删除查询命令但我需要使用这两个日期进行过滤时,它才有效。

3 个答案:

答案 0 :(得分:1)

您在创建sql语句时错过了单引号('),您的查询应如下所示: -

EntityDataSourcePersonel.CommandText =
"SELECT COUNT(TeklifTable.TeklifHazirlayan) AS Basari, EmployeeTable.Name, 
 EmployeeTable.Surname, SUM(TeklifTable.TeklifTutar) AS ToplamSatis FROM 
 EmployeeTable JOIN TeklifTable ON TeklifTable.TeklifHazirlayan = EmployeeTable.EmployeeId 
 WHERE TeklifTable.TeklifTarih >= '" + baslangicTarihi + "' 
 AND TeklifTable.TeklifTarih <= '" + bitisTarihi + "' 
 GROUP BY EmployeeTable.Name,EmployeeTable.Surname";

答案 1 :(得分:0)

我必须承认我不熟悉EntityDataSource控件。但根据what i've read,您还可以使用参数化查询。所以这样做:

String sql = @"SELECT Count(TeklifTable.TeklifHazirlayan) AS Basari, 
       EmployeeTable.NAME, 
       EmployeeTable.Surname, 
       Sum(TeklifTable.TeklifTutar)        AS ToplamSatis 
FROM   EmployeeTable 
       JOIN TeklifTable 
         ON TeklifTable.TeklifHazirlayan = EmployeeTable.EmployeeId 
GROUP  BY EmployeeTable.NAME, 
          EmployeeTable.Surname";
EntityDataSourcePersonel.CommandText = sql;
String whereClause = @"TeklifTable.TeklifTarih >= @baslangicTarihi 
                   AND TeklifTable.TeklifTarih <= @bitisTarihi";
EntityDataSourcePersonel.Where = whereClause;
EntityDataSourcePersonel.WhereParameters.Add(new Parameter("baslangicTarihi", TypeCode.DateTime,  Calendar1.SelectedDate));
EntityDataSourcePersonel.WhereParameters.Add(new Parameter("bitisTarihi", TypeCode.DateTime,  Calendar2.SelectedDate));

答案 2 :(得分:0)

结合另一个答案,指出您错过了引号和日期时间格式,以下内容应该有效:(未经过测试)

EntityDataSourcePersonel.CommandText =
"SELECT COUNT(TeklifTable.TeklifHazirlayan) AS Basari, EmployeeTable.Name, 
EmployeeTable.Surname, SUM(TeklifTable.TeklifTutar) AS ToplamSatis FROM 
EmployeeTable JOIN TeklifTable ON TeklifTable.TeklifHazirlayan = EmployeeTable.EmployeeId 
WHERE TeklifTable.TeklifTarih >= '" + baslangicTarihi.ToString("yyyy-MM-dd") + "' 
AND TeklifTable.TeklifTarih <= '" + bitisTarihi.ToString("yyyy-MM-dd") + "' 
GROUP BY EmployeeTable.Name,EmployeeTable.Surname";

还要考虑Tim关于参数化查询的观点,你应该尽可能使用它们。