简体   繁体   English

将SQL数据库中的数据加载到C#中的列表中

[英]Loading data from SQL database into a list in C#

I'm working on an application that displays (highlighted) the dates on which a certain event will occur. 我正在开发一个显示(突出显示)特定事件发生日期的应用程序。 In the database that I have to use there is a table that contains date intervals or collections of dates that have special meaning (like collection of holidays, school breaks...). 在我必须使用的数据库中,有一个表,其中包含日期间隔或具有特殊含义的日期集合(例如假期,学校休息时间的集合...)。 When an event is defined, the user defines on which day(s) that event occurs (if it occurs periodically), but the user can also define that that particular event does or does not occur on special interval. 定义事件后,用户可以定义该事件在哪一天发生(如果它定期发生),但是用户也可以定义该特定事件是否在特定间隔内发生。 For instance - there can be an event that occurs every Sunday, if that days is not a holiday. 例如-如果不是节假日,那么每个星期日都可能发生一个事件。 Now, I've solved the periodical part by reading data from the database for that event (determining on which day does that event occur) and then filling a list with dates that will be highlighted (for graphical representation I use MotnhCalendar and its BoldedDates array): 现在,我已经解决了周期性部分,方法是从数据库中读取该事件的数据(确定该事件发生在哪一天),然后用将突出显示的日期填充一个列表(对于图形表示,我使用MotnhCalendar及其BoldedDates数组):

for (DateTime day = start; day < end;day = day.AddDays(1))
{
    if (Sunday && day.DayOfWeek == DayOfWeek.Sunday)
    {
        MarkedDates.Add(day);
    }
    monthCalendar1.BoldedDates = MarkedDates.ToArray();
}

Now, for this code to work properly, I need to skip the dates that belong to a special interval (in this case if that date is a holiday). 现在,为了使此代码正常工作,我需要跳过属于特殊间隔的日期(在这种情况下,如果该日期是假日)。 It could be easily done by adding another condition into the if clause: 通过在if子句中添加另一个条件,可以轻松完成此操作:

!SpecialDates.Contains(day)

The problem is that I don't know how to fill the list with these special dates from the database. 问题是我不知道如何用数据库中的这些特殊日期来填充列表。 I can't simply "hard code" it, because these special date collections/intervals can be changed at any time. 我不能简单地对其进行“硬编码”,因为这些特殊的日期收集/间隔可以随时更改。 So, my question is - how can I, by using SQL and C# commands, read data from database and save it in the list. 因此,我的问题是-如何使用SQL和C#命令从数据库读取数据并将其保存在列表中。

Thanks 谢谢

As it looks like your using SQL Server 2008 R2 I would recommend using an ORM tool like ADO.NET Entity Framework to access your database - best going with the latest release EF 5 . 就像您使用SQL Server 2008 R2一样,我建议您使用诸如ADO.NET实体框架之类的ORM工具来访问数据库-最好使用最新版本的EF 5

There are tons of tutorials online on how to get up & running with it - a good one being Creating Model Classes with the Entity Framework . 在线上有大量关于如何启动和运行它的教程-一个很好的例子是使用Entity Framework创建模型类

To give you an idea of how simple it makes it, here is an example of the minimal amount of code you would need to achieve what it is your looking to do: 为了让您了解它的简单程度,这里有一个示例,您需要最少的代码来实现自己的目标:

using (var db = new MyDbContext())
{
    var specialDates = db.SpecialDatesTable.ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM