Build an ASP.NET Website with a SimpleDB Back End

Use Amazon's nonrelational hosted database to quickly develop a basic phone book website

For most ASP.NET developers, setting up SQL Server as the back end to your website is second nature. In general, we don't think twice about which database we'll use or the consequences of our choice. Lately, however, many alternative databases are available for consideration. The database landscape today consists of several different kinds of databases: relational databases among them Oracle, MySQL, and SQL Server; object databases; graph databases; and document databases.

The document-oriented database is an old idea that's getting a great deal of new interest lately in the web application world. Its popularity is increasing among high-traffic websites because of the fact that scaling relational databases is difficult, whereas document databases scale rather easily. Also, many simple applications don't need the full power of a relational database.

Major companies are releasing new apps that use document databases. For instance, Google heavily uses document databases internally with its BigTable database, the Apache project has released an open source document database called CouchDB, and Amazon has a cloud document database service called SimpleDB. These databases are radically different from traditional relational databases and provide great benefits for many types of applications. In this article I'll walk you through using Amazon's SimpleDB in a sample ASP.NET application to demonstrate the ease with which you can add a database-access capability to a web app. You can download the complete application by clicking the link at the end of this article online at

What Is Amazon SimpleDB?

SimpleDB is a cloud database service, which means that the database data is hosted on Amazon's servers. You can think of it as SQL Server in the cloud with one major difference: SimpleDB is not relational. Instead of storing rows with a set number of columns and keys in a table, SimpleDB stores documents or items in what's called a domain. These items have key value pairs called attributes and a unique identifier. You can kind of think of a domain as a giant Hashtable. This means there are no joins or relations between tables. For some application types, this would be a hindrance; for others, it's fine.

One interesting thing about document databases is that items in the domain aren't required to have the same types of attributes. For example, you can store cats, dogs, and automobiles in the same domain! The benefit of not having a fixed database schema is that it enables your application to be very flexible. If you need to make a change to your data, you can do so easily without a whole lot of reconfiguring of the database.

The benefits of using SimpleDB are these:

  •          It's hosted by Amazon, so you incur no database server hardware costs.
  •          SimpleDB seamlessly scales to whatever load or database size you need.
  •          SimpleDB is fast; the database handles all indexing for you, so your queries are optimized.
  •          SimpleDB has no fixed database schema, which increases development speed.
  •          The database is easy to get started with.
  •          The query syntax is simple.

Of course, SimpleDB has some weaknesses:

  •          You can't use relationships, which means you can't use joins. This is a problem because some data is just better modeled in the relational way.
  •          Complex queries and full-text searching are not yet supported.

To use SimpleDB, you need to sign up for an API key at Usage is free for most small-to-midsized applications, and then after that the price gradually scales up with your usage. Amazon has released a .NET client library that lets you interface with SimpleDB in either C# or VB.NET. You can find this library at and

Building a Phone Book Website with SimpleDB

To give you a feel for using SimpleDB as a back-end database, I'll walk you through building a phone book website with ASP.NET and SimpleDB. A phone book is a good application for a document database because there are no complex relations; the data model is pretty simple. Every phone number has a name associated with it and maybe an address or other identifying information. We'll use SimpleDB because we expect our phone book to get really popular, with thousands of users per hour and millions of numbers in our database.

Although we certainly could use SQL Server for this application, doing so would require us to think about optimizing our queries, indexing columns, maybe having multiple database servers, and replicating or sharing data. Using SimpleDB is just a lot simpler.

The first thing we need to do is create an ASP.NET project in Visual Studio and reference the Amazon.SimpleDB DLL. You'll need to include the following namespaces when using the service:

using Amazon.SimpleDB;
using Amazon.SimpleDB.Mock;
using Amazon.SimpleDB.Model;
using Amazon.SimpleDB.Util;

Now that we've configured our environment, the next step is to connect to Amazon. To authenticate yourself, you need the keys that Amazon gave you when you signed up for SimpleDB. Here's what the authentication statement looks like: 

AmazonSimpleDB client = new AmazonSimpleDBClient("key","secret_key");

 After we're authenticated, we'll be able to call all the SimpleDB operations by using the client object. These operations consist of creating and deleting domains, adding and deleting items, and querying the database.

Next we'll create a domain called phonebook. Remember that a domain is similar to a table in SQL Server, except that the domain doesn't have a fixed number of columns, column names, or column types. The call to create a domain is quite simple and can safely be called multiple times. It takes no effect if the domain has already been created.


CreateDomainRequest createRequest = new CreateDomainRequest();
createRequest.DomainName = "phonebook";


Once our domain has been created, we can begin putting our phone number data into the phonebook. After thinking about it for a while, I've decided that each item in our phonebook domain should contain a phone number, a name, and some number of categories or tags. Examples of categories we might want to use are Residential, Business, Emergency, Unlisted, and so on.

Figure 1 shows the code to add a number to our phonebook domain. Notice that we have multiple attributes named tags. In SQL Server, each column must have a unique name, but in SimpleDB we can have multiple attributes with the same name and it will be totally fine. In fact, this feature is quite convenient because in a relational database we'd generally have to create a separate table to map entry IDs with tags to replicate the same functionality.

Figure 1: Adding a number to the phonebook domain


// Put a number into the phonebook
PutAttributesRequest putRequest = new PutAttributesRequest();
List itemAttributes = new List();
itemAttributes.Add(new ReplaceableAttribute().WithName("number").WithValue(txtNumber.Text));
itemAttributes.Add(new ReplaceableAttribute().WithName("name").WithValue(txtName.Text));
string tags = txtTags.Text;
char\\[\\] delim = \\{','\\};
foreach (string tagname in tags.Split(delim))
itemAttributes.Add(new ReplaceableAttribute().WithName("tags").WithValue(tagname));
putRequest.ItemName = Guid.NewGuid().ToString();
putRequest.Attribute = itemAttributes;
putRequest.DomainName = "phonebook";
PutAttributesResponse put_resp = client.PutAttributes(putRequest);


Also notice that each item in the domain is identified by a unique globally unique identifier (GUID). We set this before we push the item to SimpleDB, like this:


putRequest.ItemName = Guid.NewGuid().ToString();


Now, of course, in the real world we'd have a batch script to upload all our phone numbers to the database, but for our purposes here we'll have a simple web form that lets you enter a number, name, and comma-separated list of tags. Figure 2 shows the add-number form.

Figure 2: Add-number form

The next part of our application will take in a phone number as input and return all the information we have for that phone number. We'll do this using the SimpleDB query functionality. Amazon was very clever in setting up a query method that mimics a normal SQL query. So ASP.NET developers familiar with SQL Server will have very little learning curve to get up to speed on querying SimpleDB.

Figure 3 contains the code to search the SimpleDB phonebook domain for a given phone number; Figure 4 shows the search-number form. Again notice the similarity between the search statement and traditional SQL statements. This is a major benefit of SimpleDB versus other document databases that have somewhat unorthodox ways of searching. The SimpleDB select request returns a group of items. In our case, we care only about the first item returned, but you can pull down many items at once. Each item has a set of attributes like those we pushed to our domain. We can simply loop through the attributes and extract the ones that we want to display. As I mentioned earlier, we'll have multiple attributes named tags, so that we can label a phone number as both EMERGENCY and GOVERNMENT, for example.


public void search(string number)
AmazonSimpleDB client = new AmazonSimpleDBClient("APIKEY", "SECRET");
string tags = "";
SelectRequest selectRequest = new SelectRequest();
selectRequest.SelectExpression = "SELECT * FROM phonebook where number = '" + number + "'
limit 1";
SelectResponse select_resp = client.Select(selectRequest);
Item item = select_resp.SelectResult.Item\\[0\\];
if (item

foreach (Amazon.SimpleDB.Model.Attribute attrib in item.Attribute)
if (attrib.Name

txtName.Text = attrib.Value;
if (attrib.Name == "tags")
tags += attrib.Value + ",";
txtTags.Text = tags;



Figure 3: Searching the SimpleDB phonebook domain for a phone number

Figure 4: Search-number form

Now that we can upload and search for numbers, our application is pretty much complete. Without thinking about DBA tasks, we can now scale up to millions of items in our database without worrying that the database will become a bottleneck in high-traffic situations. Also, if a few weeks from now we decide we want to start storing addresses along with names, we can easily add that to our put request without having to change the schema at all. You should now be starting to see the advantages that using SimpleDB offers you over using a traditional RDBMS.

Expanding Your Options

Although SQL Server is a great database for the vast majority of applications, ASP.NET developers should always be aware of other available database options. Databases like SimpleDB will only become more popular as more and more IT services move toward the cloud. Although relational databases can handle the loads of high-profile sites, the wizardry involved in optimizing your database is becoming too complex.

The newest web 2.0 sites are rethinking the way that the database is done. For sites with simple data models and those that require easy scalability, the document database is a fun little tool that ASP.NET developers should investigate.

Brian Browning ([email protected]) is a software consultant in Dallas, Texas, focused on .NET. He enjoys thinking about software development and how it can be improved.

Source code accompanying this article is available for download.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.