public void insert_audit(Audit audit) { string query = "INSERT INTO audits (id_log, name_audit, info_audit, date_audit) VALUES (@id_log, @name_audit, @info_audit, @date_audit)"; SqlDataReader reader = null; SqlCommand comm = new SqlCommand(query, db_conn); comm.Parameters.AddWithValue("@id_log", audit.id_log); comm.Parameters.AddWithValue("@name_audit", audit.name_audit); comm.Parameters.AddWithValue("@info_audit", audit.info_audit); comm.Parameters.AddWithValue("@date_audit", audit.date_audit); reader = comm.ExecuteReader(); reader.Close(); comm.Dispose(); }
public int insert_log(Log log) { string query = "INSERT INTO logs (id_user, login_date, ip) VALUES (@id_user, @login_date, @ip)"; SqlDataReader reader = null; SqlCommand comm = new SqlCommand(query, db_conn); comm.Parameters.AddWithValue("@id_user", log.id_user); comm.Parameters.AddWithValue("@login_date", log.login_date); comm.Parameters.AddWithValue("@ip", log.ip); reader = comm.ExecuteReader(); reader.Close(); comm.Dispose(); query = "SELECT * FROM logs WHERE id_user = @id_user AND logout_date IS NULL"; int id_log; comm = new SqlCommand(query, db_conn); comm.Parameters.AddWithValue("@id_user", log.id_user); try { id_log = Int32.Parse(comm.ExecuteScalar().ToString()); Audit audit = new Audit(id_log, "Login", "The user has logged in"); this.insert_audit(audit); } catch (Exception e) { id_log = 0; } reader.Close(); comm.Dispose(); return id_log; }
public void close_log(Log log) { log.logout_date = Convert.ToDateTime(DateTime.Now.ToString("HH:mm:ss.fff")); string query = "UPDATE logs SET logout_date = @logout_date WHERE id_log = @id_log"; SqlDataReader reader = null; SqlCommand comm = new SqlCommand(query, db_conn); comm.Parameters.AddWithValue("@id_log", log.id_log); comm.Parameters.AddWithValue("@logout_date", log.logout_date); reader = comm.ExecuteReader(); comm.Dispose(); reader.Close(); Audit audit = new Audit(log.id_log, "Logout", "The user has logged out"); this.insert_audit(audit); }