コード例 #1
0
ファイル: RawSql.cs プロジェクト: victorlotz/BDSA2019
        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();

                return((int)id);
            }
        }
コード例 #2
0
ファイル: RawSql.cs プロジェクト: victorlotz/BDSA2019
        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();
            }
        }