Read System TimeZone to DB


I was looking for the TimeZone Database which also need to told me if Given Timezone is DayLightSaving enabled or not. After searching few hours on Internet I found few database that do not tell the Daylight Saving stuff. Then I found a function in TimeZoneInfo class that list all System Timezone, I loop through it and got what I need.

using (SqlCommand sqlCmd = new SqlCommand(“”, Connection.MyConnection))
{
sqlCmd.CommandText = “Insert into tblTimezone (TimeZone, TimeZoneDifference, isDST) Values(@TimeZone, @TimeZoneDifference, @isDST)”;
sqlCmd.Connection = Connection.MyConnection;
sqlCmd.Connection.Open();
foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
{
sqlCmd.Parameters.Clear();
sqlCmd.Parameters.AddWithValue(“@TimeZone”, tzi.DisplayName);
sqlCmd.Parameters.AddWithValue(“@TimeZoneDifference”, tzi.BaseUtcOffset.ToString());
sqlCmd.Parameters.AddWithValue(“@isDST”, (tzi.SupportsDaylightSavingTime ? 1 : 0));
sqlCmd.ExecuteNonQuery();
}
sqlCmd.Connection.Close();
sqlCmd.Dispose();
}

Keep it simple 🙂