//寫入一筆新的LoginRecoder , 並刪除超過保留時間的Recoder internal static void InsertDelete(SwitchUserLog Log, int KeepDays) { using (SqlConnection SqlConnection = ConnectionManager.GetConnection()) { SqlCommand SqlCommand = SqlConnection.CreateCommand(); SqlCommand.CommandText = "INSERT INTO " + " MemberShip_SwitchUserLog " + " ( Id, Account, Ip, Remark, CreateTime ) " + "VALUES " + " ( @Id, @Account, @Ip, @Remark, @CreateTime ) ; " + "DELETE " + " MemberShip_SwitchUserLog " + "WHERE " + " CreateTime <= @LimitTime ; "; SqlCommand.Parameters.AddWithValue("Id", Log.Id); SqlCommand.Parameters.AddWithValue("Account", Log.Account); SqlCommand.Parameters.AddWithValue("Ip", Log.Ip); SqlCommand.Parameters.AddWithValue("Remark", Log.Remark); SqlCommand.Parameters.AddWithValue("CreateTime", Log.CreateTime); SqlCommand.Parameters.AddWithValue("LimitTime", DateTime.Now.AddDays(KeepDays * -1).ToShortDateString()); SqlConnection.Open(); SqlCommand.ExecuteNonQuery(); } }
//建立一個新的登入紀錄(因為一次登入只會建立一筆登入紀錄,所以就不採用GetNew的方式撰寫了) private static SwitchUserLog CreateSwitchUserLog(User User, string Remark) { SwitchUserLog NewSwitchUserLog = new SwitchUserLog(Guid.NewGuid().ToString(), User.Account, HttpContext.Current.Request.UserHostAddress, Remark, DateTime.Now); //寫入資料庫,並限制最多保留3天內的紀錄就好 SwitchUserAccessor.InsertDelete(NewSwitchUserLog, 3); return NewSwitchUserLog; }