You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. namespace CalcAnnualLeave
  10. {
  11. public class AnnualLeave : TimingTaskBase.TimingTaskBase
  12. {
  13. private static string connString;
  14. static AnnualLeave()
  15. {
  16. XmlDocument docConfig = new XmlDocument();
  17. docConfig.Load(System.Environment.CurrentDirectory + "/TimingTaskConfig.xml");
  18. XmlNode xnl = docConfig.SelectSingleNode(@"//Task[@ID='1']");
  19. connString = xnl.Attributes["connString"].Value;
  20. }
  21. public override void Run()
  22. {
  23. CalcAnnual();
  24. }
  25. private static void CalcAnnual()
  26. {
  27. DateTime dtNow = DateTime.Now;
  28. string strSql = @"update dbo.VocationAndJiaBanReport set ThisYearHasNian = ThisYearHasNian + 1 ";
  29. SqlConnection conn = new SqlConnection(connString);
  30. try
  31. {
  32. WriteLog("执行开始");
  33. conn.Open();
  34. SqlCommand cmmd = new SqlCommand(strSql, conn);
  35. cmmd.ExecuteNonQuery();
  36. WriteLog("ENd");
  37. }
  38. catch (Exception e)
  39. {
  40. WriteLog(e.Message);
  41. conn.Close();
  42. conn.Dispose();
  43. }
  44. finally
  45. {
  46. conn.Close();
  47. conn.Dispose();
  48. }
  49. }
  50. /// <summary>
  51. /// 写定时任务日志
  52. /// </summary>
  53. /// <param name="strMsg"></param>
  54. private static void WriteLog(string strMsg)
  55. {
  56. string strFile = @"C:/TimingTaskLog.log";
  57. object objLock = new object();
  58. if (!File.Exists(strFile))
  59. {
  60. File.Create(strFile);
  61. }
  62. lock (objLock)
  63. {
  64. StreamWriter sw = new StreamWriter(strFile, true);
  65. sw.WriteLine(DateTime.Now.ToString());
  66. sw.WriteLine("message text:" + strMsg);
  67. sw.Close();
  68. }
  69. }
  70. }
  71. }