Hmm, Don’t go for hitting database on every page refresh. That will hit badly on your performance. You caching. Add Dependency with caching data and database that when ever data will changed on a database your cache will be update. You just access cache data.
Use a DB in which save every user login/logout detail with coresponding userids.It can done by Session_start and Session_end mathod in asax file.For the count ,take a application variable,update it every time on user's when login/logout.or simply get the same from DB.
As I am seeing you are a new members of CP , First of all Welcome to CP .
Before Post in any of the CP Forum, First Please Read the Posting guide line .
Did you every try to find out in Google. Have a look what can be the modules of CRM[^]