Beispiel #1
0
        public int Create(CharacterDTO character)
        {
            var cmdText = @"INSERT Character (Name, Species, Planet, ActorId)
                            VALUES (@Name, @Species, @Planet, @ActorId);
                            SELECT SCOPE_IDENTITY()";

            using var command = new SqlCommand(cmdText, _connection);

            command.Parameters.AddWithValue("@Name", character.Name);
            command.Parameters.AddWithValue("@Species", character.Species);
            command.Parameters.AddWithValue("@Planet", character.Planet);
            command.Parameters.AddWithValue("@ActorId", character.ActorId);

            OpenConnection();

            var id = command.ExecuteScalar();

            CloseConnection();

            return((int)id);
        }
Beispiel #2
0
        public void Update(CharacterDTO character)
        {
            var cmdText = @"UPDATE Characters SET
                            Name = @Name,
                            Species = @Species,
                            Planet = @Planet,
                            ActorId = @ActorId
                            WHERE Id = @Id";

            using var command = new SqlCommand(cmdText, _connection);

            command.Parameters.AddWithValue("@Id", character.Id);
            command.Parameters.AddWithValue("@Name", character.Name);
            command.Parameters.AddWithValue("@Species", character.Species);
            command.Parameters.AddWithValue("@Planet", character.Planet);
            command.Parameters.AddWithValue("@ActorId", character.ActorId);

            OpenConnection();

            command.ExecuteNonQuery();

            CloseConnection();
        }