2
Answers

creating yearly calendar in C# by storing it in sql table

Photo of o abuzaid

o abuzaid

1y
780
1

i have medical database with some tables one of these tables is calendar table it has the fielsd as:

date     
day name 
day no of the week 
flag          // this flag indecate if this date is friday or holiday 

the user enter the year to create the 12 months   OR enter the month to create only the entred month 

 

any idea 

thanks 

O. Abuzaid 

 

how can i create calendar progrmmicly in c# 

Answers (2)

2
Photo of Sam Hobbs
56 29.4k 2.1m 1y

The day name, day number and the determinmation of the day being Friday can all be calculated. The holiday depends on your country of course and the doctor's office might decide that some days are holidays and some are not. Therefore I think it is good to have a table of holidays for the doctor's office.

You do not specify what else the calendar would be used for. For example, would the calendar be used to create appointments?

One possibility would be to use julian days. They begin at 1 for the first day of the year and go to 365 (or 366) for the last day of the year. The month and day and everything else can be calculated from the julian day. Based on the information you provided, the only thing you need is a table that lists the holidays and nothing else. If there are other requiremetns then you need to be specific.

2
Photo of Prasad Raveendran
236 8.3k 1.9m 1y

check whether this code helps

using System;
using System.Data.SqlClient;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the year to create the 12 months or enter the month to create only the entered month:");
        string input = Console.ReadLine();

        int year;
        if (int.TryParse(input, out year))
        {
            CreateCalendar(year);
        }
        else
        {
            Console.WriteLine("Invalid input. Please enter a valid year or month.");
        }

        Console.ReadLine(); // Keep console open
    }

    static void CreateCalendar(int year)
    {
        string connectionString = "your_connection_string_here"; // Replace with your actual connection string
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            for (int month = 1; month <= 12; month++)
            {
                DateTime startDate = new DateTime(year, month, 1);
                DateTime endDate = startDate.AddMonths(1).AddDays(-1);

                Console.WriteLine($"Creating calendar for {startDate.ToString("MMMM")} {year}");

                // Iterate through each day in the month
                for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
                {
                    string dayName = date.ToString("dddd");
                    int dayNumberOfWeek = (int)date.DayOfWeek;
                    bool isFriday = dayNumberOfWeek == 5;
                    bool isHoliday = /* Logic to determine if the date is a holiday */;

                    // Insert into database
                    InsertIntoCalendarTable(connection, date, dayName, dayNumberOfWeek, isFriday, isHoliday);
                }
            }

            connection.Close();
        }

        Console.WriteLine("Calendar creation completed.");
    }

    static void InsertIntoCalendarTable(SqlConnection connection, DateTime date, string dayName, int dayNumberOfWeek, bool isFriday, bool isHoliday)
    {
        string insertQuery = "INSERT INTO YourCalendarTable (date, day_name, day_no_of_the_week, flag) VALUES (@Date, @DayName, @DayNumberOfWeek, @Flag)";
        SqlCommand command = new SqlCommand(insertQuery, connection);
        command.Parameters.AddWithValue("@Date", date);
        command.Parameters.AddWithValue("@DayName", dayName);
        command.Parameters.AddWithValue("@DayNumberOfWeek", dayNumberOfWeek);
        command.Parameters.AddWithValue("@Flag", isFriday || isHoliday);
        command.ExecuteNonQuery();
    }
}

In this code:

  • Replace "your_connection_string_here" with your actual SQL Server connection string.
  • Implement the logic to determine if a particular date is a holiday according to your requirements.
  • Update "YourCalendarTable" with the name of your calendar table in the database.

This code will create the calendar for the specified year or month, inserting the relevant information into your database table.