Exemplo n.º 1
0
        public List <Student> GetStudentByName(string name)
        {
            var         students = new List <Student>();
            ObjectCache cache    = MemoryCache.Default;

            //read form cache
            if (cache.Contains("StudentsTable"))
            {
                return((List <Student>)cache.Get("StudentsTable"));
            }
            const string commandText = "Select * from Students where FirstName=@name";

            using (var connection = ConnFactory.CreateOpenConnection())
                using (var command = CmdFactory.CreateCommand(connection, commandText,
                                                              new SqlParameter("@name", name)))
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            students.Add(new Student
                            {
                                FirstName   = reader.GetString("FirstName"),
                                LastName    = reader.GetString("LastName"),
                                DateOfBirth = reader.GetDateTime("DateOfBirth")
                            });
                        }
                        //Add to cache
                        cache.Add("StudentsTable", students, new CacheItemPolicy
                        {
                            AbsoluteExpiration = DateTime.Now.AddMinutes(10)
                        });
                    }
            return(students);
        }
Exemplo n.º 2
0
 public void InsertStudent(Student student)
 {
     using (var connection = ConnFactory.CreateOpenConnection())
         using (var command = CmdFactory.CreateStoredProcedure(connection, "sp_InsertStudent",
                                                               new SqlParameter("@FirstName", student.FirstName),
                                                               new SqlParameter("@LastName", student.LastName),
                                                               new SqlParameter("@DateOfBirth", student.DateOfBirth)))
         {
             command.ExecuteNonQuery();
         }
 }