Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Example: CRUD Operations Using the AWS SDK for .NET Low-Level API

The following C# code example illustrates CRUD operations on an item. The example adds an item to the ProductCatalog table, retrieves it, performs various updates, and finally deletes the item. If you followed the steps in Creating Tables and Loading Sample Data, you already have the ProductCatalog table created. You can also create these sample tables programmatically. For more information, see Creating Example Tables and Uploading Data Using the AWS SDK for .NET.

For step-by-step instructions to test the following sample, see .NET Code Samples.

using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime;
using Amazon.SecurityToken;

namespace com.amazonaws.codesamples
    {
    class LowLevelItemCRUDExample
        {
        private static string tableName = "ProductCatalog";
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();

        static void Main(string[] args)
            {

            try
                {
                CreateItem();
                RetrieveItem();

                // Perform various updates.
                UpdateMultipleAttributes();
                UpdateExistingAttributeConditionally();

                // Delete item.
                DeleteItem();
                Console.WriteLine("To continue, press Enter");
                Console.ReadLine();
                }
            catch (Exception e)
                {
                Console.WriteLine(e.Message);
                Console.WriteLine("To continue, press Enter");
                Console.ReadLine();
                }
            }

        private static void CreateItem()
            {
            var request = new PutItemRequest
            {
                TableName = tableName,
                Item = new Dictionary<string, AttributeValue>()
        {
          { "Id", new AttributeValue { N = "1000" }},
          { "Title", new AttributeValue { S = "Book 201 Title" }},
          { "ISBN", new AttributeValue { S = "11-11-11-11" }},
          { "Authors", new AttributeValue { SS = new List<string>{"Author1", "Author2" }}},
          { "Price", new AttributeValue { N = "20.00" }},
          { "Dimensions", new AttributeValue { S = "8.5x11.0x.75" }},
          { "InPublication", new AttributeValue { BOOL = false } }
        }
            };
            client.PutItem(request);
            }

        private static void RetrieveItem()
            {
            var request = new GetItemRequest
            {
                TableName = tableName,
                Key = new Dictionary<string, AttributeValue>()
        {
          { "Id", new AttributeValue { N = "1000" } }
        },
                ProjectionExpression = "Id, ISBN, Title, Authors",
                ConsistentRead = true
            };
            var response = client.GetItem(request);

            // Check the response.
            var attributeList = response.Item; // attribute list in the response.
            Console.WriteLine("\nPrinting item after retrieving it ............");
            PrintItem(attributeList);
            }

        private static void UpdateMultipleAttributes()
            {
            var request = new UpdateItemRequest
            {
                Key = new Dictionary<string, AttributeValue>()
                {
                  { "Id", new AttributeValue { N = "1000" } }
                },
                // Perform the following updates:
                // 1) Add two new authors to the list
                // 1) Set a new attribute
                // 2) Remove the ISBN attribute
                ExpressionAttributeNames = new Dictionary<string,string>() 
                {
                    {"#A","Authors"},
                    {"#NA","NewAttribute"},
                    {"#I","ISBN"}
                },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    {":auth",new AttributeValue {SS = {"Author YY", "Author ZZ"}}},
                    {":new",new AttributeValue {S = "New Value"}}
                },

                UpdateExpression = "ADD #A :auth SET #NA = :new REMOVE #I",

                TableName = tableName,
                ReturnValues = "ALL_NEW" // Give me all attributes of the updated item.
            };
            var response = client.UpdateItem(request);

            // Check the response.
            var attributeList = response.Attributes; // attribute list in the response.
            // print attributeList.
            Console.WriteLine("\nPrinting item after multiple attribute update ............");
            PrintItem(attributeList);
            }

        private static void UpdateExistingAttributeConditionally()
            {
            var request = new UpdateItemRequest
            {
                Key = new Dictionary<string, AttributeValue>()
                {
                    { "Id", new AttributeValue { N = "1000" } }
                },
                ExpressionAttributeNames = new Dictionary<string,string>() 
                {
                    {"#P", "Price"}
                },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    {":newprice",new AttributeValue {N = "22.00"}},
                    {":currprice",new AttributeValue {N = "20.00"}}
                },
                // This updates price only if current price is 20.00.
                UpdateExpression = "SET #P = :newprice",
                ConditionExpression = "#P = :currprice",

                TableName = tableName,
                ReturnValues = "ALL_NEW" // Give me all attributes of the updated item.
            };
            var response = client.UpdateItem(request);

            // Check the response.
            var attributeList = response.Attributes; // attribute list in the response.
            Console.WriteLine("\nPrinting item after updating price value conditionally ............");
            PrintItem(attributeList);
            }

        private static void DeleteItem()
            {
            var request = new DeleteItemRequest
            {
                TableName = tableName,
                Key = new Dictionary<string, AttributeValue>()
                {
                    { "Id", new AttributeValue { N = "1000" } }
                },

                // Return the entire item as it appeared before the update.
                ReturnValues = "ALL_OLD",
                ExpressionAttributeNames = new Dictionary<string,string>() 
                {
                    {"#IP", "InPublication"}
                },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    {":inpub",new AttributeValue {BOOL = false}}
                },
                ConditionExpression = "#IP = :inpub"
            };

            var response = client.DeleteItem(request);

            // Check the response.
            var attributeList = response.Attributes; // Attribute list in the response.
            // Print item.
            Console.WriteLine("\nPrinting item that was just deleted ............");
            PrintItem(attributeList);
            }

        private static void PrintItem(Dictionary<string, AttributeValue> attributeList)
            {
            foreach (KeyValuePair<string, AttributeValue> kvp in attributeList)
                {
                string attributeName = kvp.Key;
                AttributeValue value = kvp.Value;

                Console.WriteLine(
                    attributeName + " " +
                    (value.S == null ? "" : "S=[" + value.S + "]") +
                    (value.N == null ? "" : "N=[" + value.N + "]") +
                    (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray()) + "]") +
                    (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray()) + "]")
                );
                }
            Console.WriteLine("************************************************");
            }
        }
    }