CREATE TABLE USERLOG(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
USERID INT,
LOGIN DATETIME,
LOGOUT DATETIME
)
CREATE PROCEDURE SP_TRACK_USERLOG
@USERID INT = NULL,
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (USERID,LOGIN,LOGOUT) VALUES (@USERID,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE USERID = @USERID
END
END
SqlConnection cs = new SqlConnection("YOUR CONNECTION STRING");
cs.Open();
SqlCommand cmd = new SqlCommand();
public void Login(int userID)
{
cmd.Connection = cs;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_TRACK_USERLOG";
cmd.Parameters.AddWithValue("@USERID", userID);
cmd.Parameters.AddWithValue("@COMMAND", 0);
cmd.ExecuteNonQuery();
}
public void LogOut(int userID)
{
cmd.Connection = cs;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_TRACK_USERLOG";
cmd.Parameters.AddWithValue("@USERID", userID);
cmd.Parameters.AddWithValue("@COMMAND", 1);
cmd.ExecuteNonQuery();
}