Home > Uncategorized > Running Select statements against the Amazon SimpleDB in C#

Running Select statements against the Amazon SimpleDB in C#

  public static DataTable SimpleDBSelect(string query, ref string nextToken, out bool eof)
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

             AmazonSimpleDB service = AWSClientFactory.CreateAmazonSimpleDBClient(
                    appConfig["AWSAccessKey"],
                    appConfig["AWSSecretKey"]
                    );

           

            SelectRequest queryRequest = new SelectRequest();
            queryRequest.SelectExpression = query;
            if (query == null)
            {
                eof = true;
                return null;
            }
            queryRequest.NextToken = nextToken;         // Seed for where to start reading
          
            SelectResponse queryResponse = service.Select(queryRequest);
            DataTable dt = new DataTable();
            if (queryResponse.IsSetSelectResult())
            {
                SelectResult selectResult = queryResponse.SelectResult;
                List<Item> itemList = selectResult.Item;
                dt.Clear();
                // Create the columns and name them
                dt.Columns.Add("ItemName", Type.GetType("System.String"));
                if (itemList.Count > 0)
                {
                    for (int i = 0; i < itemList[0].Attribute.Count; i++)
                    {
                        dt.Columns.Add(itemList[0].Attribute[i].Name, Type.GetType("System.String"));
                    }
                    // Now add the data
                    foreach (Item item in itemList)
                    {
                        DataRow dr = dt.NewRow();
                        List<Amazon.SimpleDB.Model.Attribute> attributeList = item.Attribute;
                        dr["ItemName"] = item.Name; ;
                        foreach (Amazon.SimpleDB.Model.Attribute attribute in attributeList)
                        {
                            dr[attribute.Name] = attribute.Value;
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            if (queryResponse.SelectResult.NextToken != null)
            {
                nextToken = CleanseToken(queryResponse.SelectResult.NextToken);
                eof = false;
            }
            else
            {
                nextToken = null;
                eof = true;
            }
            return dt;
        }

Adapted from Mike Culver’s code on Amazon. Note that SQL statements are CaSe SeNsItiVe!

Advertisements
Categories: Uncategorized
  1. Suresh
    July 24, 2012 at 12:54 pm

    Thanks. Great code.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: