public void UpdateDog(Dog dog) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" UPDATE Owner SET [Name] = @name, OwnerId = @ownerId, Breed = @breed, Notes = @notes, ImageUrl = @imageUrl WHERE Id = @id"; cmd.Parameters.AddWithValue("@name", dog.Name); cmd.Parameters.AddWithValue("@ownerId", dog.OwnerId); cmd.Parameters.AddWithValue("@breed", dog.Breed); cmd.Parameters.AddWithValue("@id", dog.Id); // nullable columns cmd.Parameters.AddWithValue("@notes", SQLHelpers.GetNullableParam(dog.Notes)); cmd.Parameters.AddWithValue("@imageUrl", SQLHelpers.GetNullableParam(dog.ImageUrl)); cmd.ExecuteNonQuery(); } } }
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", dog.Name); cmd.Parameters.AddWithValue("@ownerId", dog.OwnerId); cmd.Parameters.AddWithValue("@breed", dog.Breed); // nullable columns cmd.Parameters.AddWithValue("@notes", SQLHelpers.GetNullableParam(dog.Notes)); cmd.Parameters.AddWithValue("@imageUrl", SQLHelpers.GetNullableParam(dog.ImageUrl)); int id = (int)cmd.ExecuteScalar(); dog.Id = id; } } }