Amazon DynamoDB
开发人员指南 (API 版本 2012-08-10)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

步骤 4:将新电影添加到 Movies 表

Microsoft .NET 和 DynamoDB 教程的本步骤中,您将向 Amazon DynamoDB 中的 Movies 表添加一个新电影记录。DynamoDB_intro 中的 Main 函数首先创建一个 DynamoDB 文档模型 Document,然后等待 WritingNewMovie_async,该函数在 04_WritingNewItem.cs 文件中实施:

using System; using System.Threading.Tasks; using Amazon.DynamoDBv2.DocumentModel; namespace DynamoDB_intro { public static partial class Ddb_Intro { /*-------------------------------------------------------------------------- * WritingNewMovie *--------------------------------------------------------------------------*/ public static async Task WritingNewMovie_async( Document newItem ) { operationSucceeded = false; operationFailed = false; int year = (int) newItem["year"]; string name = newItem["title"]; if( await ReadingMovie_async( year, name, false ) ) Console.WriteLine( " The {0} movie \"{1}\" is already in the Movies table...\n" + " -- No need to add it again... its info is as follows:\n{2}", year, name, movie_record.ToJsonPretty( ) ); else { try { Task<Document> writeNew = moviesTable.PutItemAsync(newItem, token); Console.WriteLine(" -- Writing a new movie to the Movies table..."); await writeNew; Console.WriteLine(" -- Wrote the item successfully!"); operationSucceeded = true; } catch (Exception ex) { Console.WriteLine(" FAILED to write the new movie, because:\n {0}.", ex.Message); operationFailed = true; } } } } }

WritingNewMovie_async 首先检查确定新电影是否已添加到 Movies 表。如果没有,它将等待 DynamoDB Table.PutItemAsyn 方法添加新电影记录。

了解更多信息

下一步

步骤 5:从 Movies 表中读取并显示一个记录

本页内容: