Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / SQL

Azure: SQL Azure

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
9 Nov 2014CPOL3 min read 16.9K   14  
SQL Azure

This is a new post in a series of beginners' articles on how to do things in Azure. This series will be for absolute beginners, and if you are not one of those, this will not be for you.

You can find the complete set of posts that make up this series here:

This time, we will look at how to create a new SQL server database within Azure (in a later article, we will look using Microsoft's NoSQL database “DocumentDB”).

Anyway so step 1 is to open up the portal:

From there, you can click on “SQL Databases” and choose the “Create a SQL Database” hyperlink:

Image 1

From there, you need to fill in your preferences within the wizard:

Image 2

Image 3

Once this wizard has completed, you will see a new database has been created.

Image 4

IMPORTANT: When the database is created, you will need to ensure that the standard port 1433 is opened. One of the easiest ways to do that is to use the Azure portal to query the database (even though there are no tables in the database yet)

Image 5

This little cheat will prompt you to open up the Firewall ports, which is great, let's just let the Azure portal do this work for us.

Image 6

So once the port is open, you will be redirected to an app in the browser (Silverlight app at present), that allows you to use the connection details you chose.

Image 7

When you successfully connect, you should see something like this:

Image 8

Now there is no data in the SQL database yet. We could use this Silverlight app to add some tables, and data. However, I would prefer to do that in Visual Studio, so let's go back to the portal, and open the connection strings, as shown below:

Image 9

Image 10

We are interested in the ADO.NET one, where the part I have highlighted is the important part you need

Image 11

SO grabbing the connection address to the Azure SQL server instance, let's connect via Visual Studio, and create a table:

Image 12

Once you have a connection in Visual Studio, let's create a new table using the context menu:

Image 13

When you are happy with the table, click the “Update” button which will push the changes to Azure. This is only a demo, for a real app, you would likely have some sort of scripts, or would use the Entity Framework migrations facility to manage changes.

Image 14

Image 15

So now let's check everything worked by connecting to the Azure SQL database from SQL server management studio.

Image 16

As we can see, we see the table we just created above:

Image 17

And let's also check the Azure portal query app.

Image 18

Yep, the table looks good, there is no data there yet, as expected for a new table. So let's now turn our attention to getting some data into the new table.

Let's use a new Entity Framework model to talk to the new SQL Azure database/table we just created.

Image 19

I went with the defaults but you can choose what you like:

Image 20

This will result in a few files being created in the demo app, such as these, as well as an entry in the App.Config file to point to the SQL Azure database instance.

Image 21

And here is some code that will do some basic CRUD operation using the Entity Framework context that was created for us.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SQLAzureTest
{
    class Program
    {
        static void Main(string[] args)
        { 
            //insert
            using (var sachaAzureSQLEntities = new SachaAzureSQLEntities())
            {
                sachaAzureSQLEntities.Orders.Add(new Order()
                {
                    //note we are spelling this wrong so we can update it later
                    Description = "Buzz Lighyear toy",
                    Quanitity = 1
                });
                sachaAzureSQLEntities.SaveChanges(); 
 
                //select
                var order = sachaAzureSQLEntities.Orders.Single(
                    x => x.Description == "Buzz Lighyear toy");
                Console.WriteLine("Order : Id: {0}, Description: {1}, Quanity {2}",
                    order.Id, order.Description, order.Quanitity);
 
                //update
                order.Description = "Buzz Lightyear toy";
                sachaAzureSQLEntities.SaveChanges();
 
                var exists = sachaAzureSQLEntities.Orders.Any(
                    x => x.Description == "Buzz Lighyear toy");
                Console.WriteLine("Buzz Lighyear toy exists :  {0}", exists);
 
                order = sachaAzureSQLEntities.Orders.Single(
                    x => x.Description == "Buzz Lightyear toy");
                Console.WriteLine("Order : Id: {0}, Description: {1}, Quanity {2}",
                    order.Id, order.Description, order.Quanitity); 
 
                //delete
                sachaAzureSQLEntities.Orders.Remove(order);
                sachaAzureSQLEntities.SaveChanges();
 
                Console.WriteLine("Orders count :  {0}", 
                    sachaAzureSQLEntities.Orders.Count()); 
            }
 
            Console.ReadLine();
        }
    }
}

And here are the results of this against the SQL Azure instance we just created.

Image 22

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
-- There are no messages in this forum --