예제 #1
0
        public static List <CatModel> GetCats()
        {
            var ret = new List <CatModel>();

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                SqlCommand command = new SqlCommand("SELECT ID, Name, Legs, HasTail, Type FROM dbo.Cats", connection);

                connection.Open();
                SqlDataReader reader = null;

                try
                {
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        var cat = new CatModel();
                        cat.ID      = int.Parse(reader["ID"].ToString());
                        cat.Name    = reader["Name"].ToString();
                        cat.Legs    = int.Parse(reader["Legs"].ToString());
                        cat.HasTail = bool.Parse(reader["HasTail"].ToString());
                        cat.Type    = reader["Type"].ToString();
                        ret.Add(cat);
                    }
                }
                finally
                {
                    if (reader != null && !reader.IsClosed)
                    {
                        reader.Close();
                    }
                }
            }

            return(ret);
        }