Just want to write out the code for generating weekly reports. It is very boring to run the queries manually then paste in excel and send it out.
Code Snippet
- using System;
- using System.Data.OracleClient;
- using System.Text;
- namespace WeeklyReports
- {
- public static class DateTimeExtensions
- {
- public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { int diff = dt.DayOfWeek - startOfWeek; if (diff < 0) { diff += 7; } return dt.AddDays(-1 * diff).Date; }
- }
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- StringBuilder allTables = new StringBuilder();
- using (OracleConnection oraConn = new OracleConnection(@"Data Source=abc.world;Persist Security Info=True;User ID=abc;Password=abc;Unicode=True"))
- {
- DateTime monday = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
- DateTime sunday = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
- monday = monday.AddDays(-7);
- for (int ctr = 0; ctr < 10; ctr++)
- {
- String strMonday = FormatDateToOracle(monday);
- String strSunday = FormatDateToOracle(sunday);
- OracleCommand oraCmd = new OracleCommand(@"SELECT applicationname, COUNT(*) FROM EM_MONITOR.LOAD_DOT_NET_LOG_ERROR_PRD WHERE error_date BETWEEN '" + strMonday + "' AND '" + strSunday + "' AND applicationname!='(null)' GROUP BY applicationname ORDER BY COUNT(*) DESC ", oraConn);
- // OracleCommand oraCmd = new OracleCommand(@"select * from ( SELECT COUNT(*), MESSAGE FROM EM_MONITOR.LOAD_DOT_NET_LOG_ERROR_PRD WHERE error_date BETWEEN '" + strMonday + "' AND '" + strSunday + "' AND applicationname!='(null)' GROUP BY MESSAGE ORDER BY COUNT(*) DESC ) where rownum <= 5 ", oraConn);
- oraConn.Open();
- OracleDataReader oraDr = oraCmd.ExecuteReader();
- StringBuilder sbTable = new StringBuilder();
- StringBuilder sbRows = new StringBuilder();
- while (oraDr.Read())
- {
- sbRows.Append("");
- for (int col = 0; col < oraDr.FieldCount; col++)
- {
- sbRows.Append("" + oraDr[col].ToString() + "");
- }
- sbRows.Append("");
- }
- sbTable.Append("" + sbRows + "
" + "For week " + strMonday + " - " + strSunday + " - allTables.Append(sbTable.ToString() + @"
"); - oraConn.Close();
- monday = monday.AddDays(-7);
- sunday = sunday.AddDays(-7);
- }
- }
- divTables.InnerHtml = allTables.ToString();
- }
- private string FormatDateToOracle(DateTime date)
- {
- return date.ToString("dd-MMM-yyyy").ToUpper();
- }
- }
- }
No comments:
Post a Comment