简体   繁体   English

显示日期不在数据库中,仅显示一周中的特定日期

[英]Display dates not in database, only specific days of the week

I have a sql database filled with specific dates for weekly training. 我有一个包含特定日期的sql数据库,用于每周培训。 We only have weekly training on Tuesdays and Wednesdays. 我们仅在星期二和星期三进行每周培训。 It currently outputs like this: 当前输出如下:

Training Schedule

Mar 4 - Training Title
Mar 5 - Training Title
Mar 12 - Training Title
Mar 19 - Training Title

Well now I am being asked to show dates that aren't in the database as well but only Tuesday's and Wednesdays. 现在,我被要求显示不在数据库中的日期,而仅显示星期二和星期三。 So basically it has to look like this now. 因此,基本上现在它必须看起来像这样。

Training Schedule

Mar 4 - Training Title
Mar 5 - Training Title
Mar 12 - Training Title
Mar 13 - No Training
Mar 19 - Training Title
Mar 20 - No Training

What is the best way to do this without going in and adding all the 'No Training' dates to the database (because that would take forever). 无需输入所有“无培训”日期并将其添加到数据库中的最佳方法是什么(因为这将永远花费)。 Right now I'm simply just outputting the database results using the following code: 现在,我只是使用以下代码输出数据库结果:

var now = DateTime.Now;
var tableSql = "select * from Training where Date >= " + "'" + now + "'" + " order by Date";
pcm.CommandText = tableSql;
prs = pcm.ExecuteReader();
var rowcount = 0;
while (prs.Read())
   {
     rowcount++;
          %>
 <%=Convert.ToDateTime(prs["Date"].ToString()).ToString("MMM d").ToUpper()%> - <%=prs["Title"].ToString().ToUpper() %> <% } %>
        <%
            prs.Close();
            pcn.Close();
        %>

Loop through the dataset returned by the SQL query and add the results to a collection ( List<string> ). 循环浏览由SQL查询返回的数据集,并将结果添加到集合( List<string> )中。 Then take your known date range and loop through those dates and if the day of week is Tuesday or Thursday, check your collection to see if there is a value for that date. 然后获取您的已知日期范围并遍历这些日期,如果星期几是星期二或星期四,请检查您的集合以查看该日期是否有值。 If so, display that string, if not, display the "No Training" message. 如果是这样,则显示该字符串,否则,显示“ No Training”消息。

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

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