Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. //Creating list object for UserConnection management
  2. private static List<UserConnection> _listUserConnetion = new List<UserConnection>();
  3. //Local variables
  4. private bool _firstInit = true;
  5. private static int _second = 5;
  6.  
  7. //Default constructor
  8. //Initializing Timer to get the frequent data
  9. //From database
  10. public SignalRChat()
  11. {
  12. //Timer object creation
  13. Timer aTimer = new Timer();
  14. //Binding elapsed event
  15. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  16. //Initializing interval
  17. aTimer.Interval = _second * 1000;
  18. aTimer.Enabled = true;
  19. _firstInit = false;
  20. aTimer.Start();
  21. }
  22.  
  23. #region TimerEvent
  24. //Calling function to get the data from database
  25. private void OnTimedEvent(object sender, ElapsedEventArgs e)
  26. {
  27. if (!_firstInit)
  28. SendData(Context.ConnectionId);
  29. }
  30. #endregion TimerEvent
  31.  
  32. //Sending data to all connected clients
  33. public void SendData(string ConnectionId)
  34. {
  35. var user = _listUserConnetion.Where(o => o.ConnectionID == ConnectionId);
  36. if (user.Any())
  37. {
  38. //Using the Clients property of IHubCallerConnectionContext from hub abstract class
  39. Clients.All.sendMessage(FetchData());
  40. }
  41. }
  42.  
  43. #region Bind Data
  44. //Method is use to Fetch data from database
  45. public List<StockExchange> FetchData()
  46. {
  47. //connection object
  48. OleDbConnection conn = null;
  49. //StockExchange list object
  50. List<StockExchange> listStudent = new List<StockExchange>();
  51. //string holding the FilePath
  52. string FilePath = @"D:\Auth\SignalR\SignalR\Excel\ExcelData.xlsx";
  53. //connection string for OleDB connection
  54. string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0;ReadOnly=true;HDR=Yes;\"";
  55. //SQL Command
  56. string Command = "SELECT TOP 4 Id, CompanyName, Price FROM [Sheet1$] ORDER BY Id Desc";
  57. try
  58. {
  59. using (conn = new OleDbConnection(ConnectionString))
  60. {
  61. using (OleDbCommand cmd = new OleDbCommand(Command, conn))
  62. {
  63. conn.Open();
  64. using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
  65. {
  66. DataSet id = new DataSet();
  67. da.Fill(id);
  68.  
  69. DataTable idtable = id.Tables[0];
  70.  
  71. listStudent = (from s in idtable.AsEnumerable()
  72. select new StockExchange
  73. {
  74. Id = Convert.ToInt32(s["Id"].ToString()),
  75. Name = s["CompanyName"].ToString(),
  76. Price = Convert.ToDouble(s["Price"])
  77. }).ToList();
  78.  
  79. return listStudent;
  80. }
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. WriteLogFile.WriteLog(ex.Message);
  87. }
  88. finally
  89. {
  90. if (conn != null || conn.State == ConnectionState.Open)
  91. {
  92. conn.Close();
  93. }
  94. }
  95. return listStudent;
  96. }
  97. #endregion Bind Data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement