【发布时间】:2009-04-19 15:32:07
【问题描述】:
我正在尝试从事件数据库(EventDate 字段作为日期)中选择 EventDate 为当前月份的记录。
我有计算当月第一天的函数:
public string GetFirstofMonth(DateTime dt)
{
int thisMonth = dt.Month;
int thisYear = dt.Year;
string firstOfMonth = thisMonth.ToString() + "/1/" + thisYear.ToString();
return firstOfMonth;
}
以及当月的最后一天:
public string GetLastofMonth(DateTime dt)
{
DateTime Date2Obj = new DateTime(dt.Year, dt.Month, 1);
int thisMonth = dt.Month;
int thisYear = dt.Year;
Date2Obj.AddMonths(1);
Date2Obj.AddDays(-1);
int lastDay = Date2Obj.Day;
string LastOfMonth = thisMonth + "/" + lastDay + "/" + thisYear;
return LastOfMonth;
}
所以在 MasterPage(或实例,没关系)我有这个 SQLDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MeetingsConnectionString %>"
SelectCommand="SELECT * FROM [Events] WHERE (([EventDate] >= @EventDate) AND ([EventDate] <= @EventDate2))">
<SelectParameters>
<asp:Parameter DbType="Date" Name="EventDate" />
<asp:Parameter DbType="Date" Name="EventDate2" />
</SelectParameters>
</asp:SqlDataSource>
如何修改 SQLDataSource 中的 EventDate 和 EventDate2 参数以使用 GetFirstofMonth(DateTime.Today);和 GetLastofMonth(DateTime.Today) 分别作为它们的值?或者这不可能?
【问题讨论】:
-
谢谢大家的回答。何塞工作了,所以我和他一起去,但你们都得到了提升。
标签: c# asp.net sql parameters