是否可以使用cc中的monthcalendar在sql中重新保存存储日期 [英] Is there aposibility of retreaving stored dates in sql with monthcalendar in c#
问题描述
任何人都可以帮助C#编程的新手,我正在尝试将存储的SQL日期从表中加载到C#中的monthCalendar。到目前为止我的代码并不知道如何实现加载Event_SchedulerTable的Event_Start_Date列:
void CalendarRefresh()
{
Connection con1 = new Connection();
SqlCommand sda = new SqlCommand( SELECT Event_Start_Date FROM Event_SchedulerTable,con1.ActiveCon());
DataTable dt = new DataTable();
SqlDataReader reader = sda.ExecuteReader();
}
我也是喜欢调用AddBoldedDates()函数将存储在SQL日期中的日期加粗到monthCalendar上。请任何人帮助我。
public void AddBoldedDates()
{
List< DateTime> listBoldedDates = new List< DateTime>();
for (DateTime m = monthCalendarDate.SelectionEnd; m < = monthCalendarDate.SelectionEnd; m = m.AddDays( 1 ))
{
listBoldedDates.Add(DateTime.Parse(m.ToShortDateString()));
}
monthCalendarDate.BoldedDates = new DateTime [] {};
listBoldedDates.AddRange(monthCalendarDate.BoldedDates); // 如果您想保留以前添加的日期,请将以前添加的日期添加到列表
monthCalendarDate.BoldedDates = listBoldedDates.ToArray();
}
我的尝试:
我在网上浏览过显示存储的SQL日期的解决方案,但它们看起来有点复杂,有些仅适用于asp.net
这是一个工作示例,演示如何生成适合分配给MonthCalendar.BoldedDates属性的DateTime数组:private DateTime [] CreateDateRange (DateTime start,DateTime end,Func< DateTime,bool> validdatefunc)
{
List< DateTime> dates = new List< DateTime>();
// 处理降序日期顺序
if (start > end)
{
DateTime temp = start;
start = end;
end = temp;
}
// 最好抛出错误?
// 注意:MonthCalendar.BoldedDates可以设置为'null without error
if ((end - start).Days == 0 ) return null ;
for (DateTime d = start; d < = end; d = d.AddDays( 1 ))
{
if (validdatefunc(d ))dates.Add(d);
}
return dates.ToArray();
}以下是样本使用的样子:
// 在某些Method或EventHandler中:
DateTime d1 = new DateTime( 2016 , 2 , 1 );
DateTime d2 = new DateTime( 2016 , 2 , 29 );
// 评估日期的示例函数
// 测试Days值是否为模#3
Func< DateTime,bool> adatefunc =(日期时间日期)= >
{
返回 date.Day %3 == 0 ;
};
monthCalendar1.BoldedDates = CreateDateRange(d2,d1,adatefunc);我在为'DateTime(此处未显示)编写的扩展方法中使用类似的逻辑。
Anyone can help as new to C# programming and I'm trying to load stored SQL dates from a table to a monthCalendar in C#. The code I have so far and don't know how to implement loading the Event_Start_Date column of the Event_SchedulerTable:
void CalendarRefresh()
{
Connection con1 = new Connection();
SqlCommand sda = new SqlCommand("SELECT Event_Start_Date FROM Event_SchedulerTable", con1.ActiveCon());
DataTable dt = new DataTable();
SqlDataReader reader = sda.ExecuteReader();
}
I also like to call the AddBoldedDates() function to bold the dates stored in the SQL dates onto the monthCalendar. Please can anyone help me.
public void AddBoldedDates()
{
List<DateTime> listBoldedDates = new List<DateTime>();
for (DateTime m = monthCalendarDate.SelectionEnd; m <= monthCalendarDate.SelectionEnd; m = m.AddDays(1))
{
listBoldedDates.Add(DateTime.Parse(m.ToShortDateString()));
}
monthCalendarDate.BoldedDates = new DateTime[] { };
listBoldedDates.AddRange(monthCalendarDate.BoldedDates); //If you want to preserve previously added dates then add previously added dates to list as
monthCalendarDate.BoldedDates = listBoldedDates.ToArray();
}
What I have tried:
I have looked around on the web for solutions to display stored SQL dates but they seem a bit complex and some just for asp.net
Here's a working example that shows how to generate an Array of DateTime suitable for assignment to the MonthCalendar.BoldedDates Property:private DateTime[] CreateDateRange(DateTime start, DateTime end, Func<DateTime, bool> validdatefunc) { List<DateTime> dates = new List<DateTime>(); // handle descending date order if (start > end) { DateTime temp = start; start = end; end = temp; } // better to throw an error ? // note: MonthCalendar.BoldedDates can be set to 'null without error if ((end - start).Days == 0) return null; for (DateTime d = start; d <= end; d = d.AddDays(1)) { if (validdatefunc(d)) dates.Add(d); } return dates.ToArray(); }Here's what sample use might look like:
// in some Method or EventHandler: DateTime d1 = new DateTime(2016, 2, 1); DateTime d2 = new DateTime(2016, 2, 29); // sample function to evaluate a date // tests if the Days value is modulo #3 Func<DateTime, bool> adatefunc = (DateTime date) => { return date.Day%3 == 0; }; monthCalendar1.BoldedDates = CreateDateRange(d2, d1, adatefunc);I use similar logic in an Extension Method I wrote for 'DateTime (not shown here).
这篇关于是否可以使用cc中的monthcalendar在sql中重新保存存储日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!