コード例 #1
0
        public static Movie GetById(int id)
        {
            var sql = "SELECT [Id], [Title], [ReleaseYear] FROM [Movies] WHERE [Id] = " + id;

            using (var connection = new SqlConnection(connString))
            {

                using (var cmdSql = new SqlCommand(sql, connection))
                {

                    cmdSql.CommandTimeout = 3000;
                    cmdSql.CommandType = CommandType.Text;
                    connection.Open();
                    using (var rdr = cmdSql.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            var movie = new Movie
                            {
                                id = rdr.GetInt32(0),
                                title = rdr.GetString(1),
                                releaseYear = rdr.GetInt32(2)
                            };
                            return movie;
                        }
                    }
                }

            }

            return null;
        }
コード例 #2
0
        public static Movie Update(Movie movie)
        {
            const string sql = "UPDATE [Movies] SET [Title] = @Title, [ReleaseYear] = @RYear WHERE [Id] = @Id";

            var paramList = new List<SqlParameter>
            {
                new SqlParameter("@Title", movie.title),
                new SqlParameter("@RYear", movie.releaseYear),
                new SqlParameter("@Id", movie.id)
            };

            if (ExecuteInLineSql(sql, paramList))
            {
                return movie;
            }
            else
            {
                return null;
            }

        }