Tuesday, January 24, 2012

Weekly reports that needs to be sent manually.


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
  1. using System;
  2. using System.Data.OracleClient;
  3. using System.Text;
  4.  
  5. namespace WeeklyReports
  6. {
  7.  
  8.     public static class DateTimeExtensions
  9.     {
  10.         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; }
  11.     }
  12.  
  13.  
  14.     public partial class _Default : System.Web.UI.Page
  15.     {
  16.  
  17.  
  18.  
  19.         protected void Page_Load(object sender, EventArgs e)
  20.         {
  21.  
  22.             StringBuilder allTables = new StringBuilder();
  23.  
  24.             using (OracleConnection oraConn = new OracleConnection(@"Data Source=abc.world;Persist Security Info=True;User ID=abc;Password=abc;Unicode=True"))
  25.             {
  26.  
  27.                 DateTime monday = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
  28.                 DateTime sunday = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
  29.                 monday = monday.AddDays(-7);
  30.  
  31.                 for (int ctr = 0; ctr < 10; ctr++)
  32.                 {
  33.  
  34.  
  35.  
  36.                     String strMonday = FormatDateToOracle(monday);
  37.                     String strSunday = FormatDateToOracle(sunday);
  38.  
  39.                    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);
  40.  
  41.                   //  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);
  42.  
  43.                     oraConn.Open();
  44.                     OracleDataReader oraDr = oraCmd.ExecuteReader();
  45.  
  46.                     StringBuilder sbTable = new StringBuilder();
  47.  
  48.                     StringBuilder sbRows = new StringBuilder();
  49.  
  50.                     while (oraDr.Read())
  51.                     {
  52.  
  53.                         sbRows.Append("");
  54.                         for (int col = 0; col < oraDr.FieldCount; col++)
  55.                         {
  56.                             sbRows.Append("" + oraDr[col].ToString() + "");
  57.  
  58.                         }
  59.  
  60.                         sbRows.Append("");
  61.  
  62.                     }
  63.  
  64.                     sbTable.Append("" + sbRows + "
    "  + "For week " +  strMonday + " - " + strSunday + "
    "
    );
  65.  
  66.                     allTables.Append(sbTable.ToString() + @"");
  67.                     oraConn.Close();
  68.  
  69.                     monday = monday.AddDays(-7);
  70.                     sunday = sunday.AddDays(-7);
  71.  
  72.                 }
  73.             }
  74.  
  75.             divTables.InnerHtml  = allTables.ToString();
  76.  
  77.         }
  78.  
  79.         private string FormatDateToOracle(DateTime date)
  80.         {
  81.             return date.ToString("dd-MMM-yyyy").ToUpper();
  82.         }
  83.  
  84.       
  85.     }
  86. }

No comments:

Post a Comment