Somen Natta

Somen Natta

  • NA
  • 33
  • 3.3k

insert csv file into sql table using windows service

Apr 21 2021 10:29 AM
I have a method A1 in the class service2021.cs which is not being either called or executed. Can anyone tell me why?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Diagnostics;  
  7. using System.Linq;  
  8. using System.ServiceProcess;  
  9. using System.Text;  
  10. using System.Threading.Tasks;  
  11. using System.Configuration;  
  12. using System.IO;  
  13. using System.Timers;  
  14. namespace InsertCsvIntoDatabaseTable  
  15. {  
  16. public partial class Service2021 : ServiceBase  
  17. {  
  18. Timer timer = new Timer();  
  19. public Service2021()  
  20. {  
  21. InitializeComponent();  
  22. //A1();  
  23. }  
  24. protected override void OnStart(string[] args)  
  25. {  
  26. WriteToFile("Service is started at " + DateTime.Now);  
  27. timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);  
  28. WriteToFile("After ElapsedEventHandler in OnStart " + DateTime.Now);  
  29. timer.Interval = 30000;  
  30. WriteToFile("After timer Interval in OnStart " + DateTime.Now);  
  31. timer.Enabled = true;  
  32. WriteToFile("After timer Enabled in OnStart " + DateTime.Now);  
  33. }  
  34. protected override void OnStop()  
  35. {  
  36. WriteToFile("Service is stopped at " + DateTime.Now);  
  37. }  
  38. private void OnElapsedTime(object source, ElapsedEventArgs e)  
  39. {  
  40. //WriteToFile("Service is recall at before A1 method" + DateTime.Now);  
  41. A1();  
  42. WriteToFile("Service is recall at after A1 method" + DateTime.Now);  
  43. }  
  44. public void WriteToFile(string Message)  
  45. {  
  46. string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";  
  47. if (!Directory.Exists(path))  
  48. {  
  49. Directory.CreateDirectory(path);  
  50. }  
  51. string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/''_') + ".txt";  
  52. if (!File.Exists(filepath))  
  53. {  
  54. // Create a file to write to.  
  55. using (StreamWriter sw = File.CreateText(filepath))  
  56. {  
  57. sw.WriteLine(Message);  
  58. }  
  59. }  
  60. else  
  61. {  
  62. using (StreamWriter sw = File.AppendText(filepath))  
  63. {  
  64. sw.WriteLine(Message);  
  65. }  
  66. }  
  67. }  
  68. public void A1()  
  69. {  
  70. Class2 ob = new Class2();  
  71. //ob.InsertExcelRecords(CSVFilePath);  
  72. ob.InsertExcelRecords();  
  73. }  
  74. }  
  75. }  

Answers (2)