Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Creating list object for UserConnection management
- private static List<UserConnection> _listUserConnetion = new List<UserConnection>();
- //Local variables
- private bool _firstInit = true;
- private static int _second = 5;
- //Default constructor
- //Initializing Timer to get the frequent data
- //From database
- public SignalRChat()
- {
- //Timer object creation
- Timer aTimer = new Timer();
- //Binding elapsed event
- aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
- //Initializing interval
- aTimer.Interval = _second * 1000;
- aTimer.Enabled = true;
- _firstInit = false;
- aTimer.Start();
- }
- #region TimerEvent
- //Calling function to get the data from database
- private void OnTimedEvent(object sender, ElapsedEventArgs e)
- {
- if (!_firstInit)
- SendData(Context.ConnectionId);
- }
- #endregion TimerEvent
- //Sending data to all connected clients
- public void SendData(string ConnectionId)
- {
- var user = _listUserConnetion.Where(o => o.ConnectionID == ConnectionId);
- if (user.Any())
- {
- //Using the Clients property of IHubCallerConnectionContext from hub abstract class
- Clients.All.sendMessage(FetchData());
- }
- }
- #region Bind Data
- //Method is use to Fetch data from database
- public List<StockExchange> FetchData()
- {
- //connection object
- OleDbConnection conn = null;
- //StockExchange list object
- List<StockExchange> listStudent = new List<StockExchange>();
- //string holding the FilePath
- string FilePath = @"D:\Auth\SignalR\SignalR\Excel\ExcelData.xlsx";
- //connection string for OleDB connection
- string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0;ReadOnly=true;HDR=Yes;\"";
- //SQL Command
- string Command = "SELECT TOP 4 Id, CompanyName, Price FROM [Sheet1$] ORDER BY Id Desc";
- try
- {
- using (conn = new OleDbConnection(ConnectionString))
- {
- using (OleDbCommand cmd = new OleDbCommand(Command, conn))
- {
- conn.Open();
- using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
- {
- DataSet id = new DataSet();
- da.Fill(id);
- DataTable idtable = id.Tables[0];
- listStudent = (from s in idtable.AsEnumerable()
- select new StockExchange
- {
- Id = Convert.ToInt32(s["Id"].ToString()),
- Name = s["CompanyName"].ToString(),
- Price = Convert.ToDouble(s["Price"])
- }).ToList();
- return listStudent;
- }
- }
- }
- }
- catch (Exception ex)
- {
- WriteLogFile.WriteLog(ex.Message);
- }
- finally
- {
- if (conn != null || conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- }
- return listStudent;
- }
- #endregion Bind Data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement