Beispiel #1
0
        public List <Dog> GetDogs()
        {
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
                        SELECT Id, Name, OwnerId, Breed, Notes, ImageUrl
                        FROM Dog
                    ";

                    SqlDataReader reader = cmd.ExecuteReader();

                    List <Dog> dogs = new List <Dog>();
                    while (reader.Read())
                    {
                        Dog dog = new Dog
                        {
                            Id       = reader.GetInt32(reader.GetOrdinal("Id")),
                            Name     = reader.GetString(reader.GetOrdinal("Name")),
                            OwnerId  = reader.GetInt32(reader.GetOrdinal("OwnerId")),
                            Breed    = reader.GetString(reader.GetOrdinal("Breed")),
                            Notes    = ReaderUtils.GetNullableString(reader, "Notes"),
                            ImageUrl = ReaderUtils.GetNullableString(reader, "ImageUrl"),
                        };

                        dogs.Add(dog);
                    }

                    reader.Close();

                    return(dogs);
                }
            }
        }
Beispiel #2
0
        public void AddDog(Dog dog)
        {
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
                    INSERT INTO Dog ([Name], OwnerId, Breed, Notes, ImageUrl)
                    OUTPUT INSERTED.ID
                    VALUES (@name, @ownerId, @breed, @notes, @imageUrl);
                ";
                    cmd.Parameters.AddWithValue("@name", ReaderUtils.GetNullableParam(dog.Name));
                    cmd.Parameters.AddWithValue("@ownerId", ReaderUtils.GetNullableParam(dog.OwnerId));
                    cmd.Parameters.AddWithValue("@breed", ReaderUtils.GetNullableParam(dog.Breed));
                    cmd.Parameters.AddWithValue("@notes", ReaderUtils.GetNullableParam(dog.Notes));
                    cmd.Parameters.AddWithValue("@imageUrl", ReaderUtils.GetNullableParam(dog.ImageURL));

                    int id = (int)cmd.ExecuteScalar();

                    dog.Id = id;
                }
            }
        }