SQL Azure – Relational Database as a Service – Soup to Nuts – The Whole Enchilada
This post will be very direct. It will get a database running in the Azure cloud as quickly as possible. There is plenty of material about why SQL Azure might be of interest and how it works. Go to Bing and type SQL Azure and you’ll get all the reasons that SQL Azure is the right solution in many scenarios. www.microsoftpdc.com is a great place to start.
Brief Goals
- Build a simple data driven ASP.NET Page using the graphical controls in Visual Studio 2008
- Develop against a local SQL Server/SQL Express database before connecting to SQL Azure
Steps in this post
- Create the northwind database
- Add northwind to sql server
- Create a cloud project with 1 web role
- Add a grid and attach grid’s data source our data (Northwind database)
- Run our cloud application against local data
- Create a SQL Azure database
- Download and install the SQL Server Migration Wizard
- Upload the customers table to SQL Azure
- Change our connection string to point our app to the cloud
- Run our app and verify everything works using the data from SQL Azure (NorthwindInCloud)
The Northwind database – where to download
Do a search on Bing for “Northwind and pubs Sample Databases for SQL Server 2000” and you should find this link:
The Northwind database – building the database
The file to pay attention to in the SQL Server 2000 Sample databases is instnwnd.sql.
This is only a fraction of the .sql code.
Code Snippet
- SET NOCOUNT ON
- GO
- USE master
- GO
- if exists (select * from sysdatabases where name='Northwind')
- drop database Northwind
- go
- DECLARE @device_directory NVARCHAR(520)
- SELECT @device_directory = SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1)
- FROM master.dbo.sysaltfiles WHERE dbid = 1 AND fileid = 1
- EXECUTE (N'CREATE DATABASE Northwind
- ON PRIMARY (NAME = N''Northwind'', FILENAME = N''' + @device_directory + N'northwnd.mdf'')
- LOG ON (NAME = N''Northwind_log'', FILENAME = N''' + @device_directory + N'northwnd.ldf'')')
- go
- exec sp_dboption 'Northwind','trunc. log on chkpt.','true'
- exec sp_dboption 'Northwind','select into/bulkcopy','true'
- GO
- set quoted_identifier on
- GO
- /* Set DATEFORMAT so that the date strings are interpreted correctly regardless of
- the default DATEFORMAT on the server.
- */
- SET DATEFORMAT mdy
- GO
- use "Northwind"
- go
- if exists (select * from sysobjects where id = object_id('dbo.Employee Sales by Country') and sysstat & 0xf = 4)
- drop procedure "dbo"."Employee Sales by Country"
- GO
- if exists (select * from sysobjects where id = object_id('dbo.Sales by Year') and sysstat & 0xf = 4)
- drop procedure "dbo"."Sales by Year"
- GO
- if exists (select * from sysobjects where id = object_id('dbo.Ten Most Expensive Products') and sysstat & 0xf = 4)
- drop procedure "dbo"."Ten Most Expensive Products"
- GO
Microsoft SQL Server Management Studio
Purpose of Screen: To create a new database |
Start Microsoft SQL Server Management Studio and choose, “File, Open, File”
Execute the Query to build the database
Open instnwnd.sql and go to SQL Server Management Studio and hit the “f5” key or go to the menu and choose “Query, Execute.”
Notice that we have a Northwind database
We will work with some of the tables here in our sample application.
NorthWind Database – Attach to “Server Explorer”
One of the first tasks we need to get done is get a database created and ready to code against within Visual Studio 2010.
Purpose of Screen: Open "Server Explorer" |
Adding a connection to Northwind
Purpose of Screen: Add Northwind to Server Explorer |
Northwind
Purpose of Screen: Add a database connection |
Advanced connection properties
Purpose of Screen: Demonstrate the local connection string |
Task – Creating the Visual Studio Project
In this task you will create a new Visual Studio project for the Windows Azure Web Site:
Open Microsoft Visual Studio 2008/2010 elevated as Administrator, from Start | All Programs | Microsoft Visual Studio 2008/2010 right-click Microsoft Visual Studio 2008/2010 and choose Run as Administrator.
If the User Account Control dialog appears, click Continue.
From the File menu, choose New and then Project.
Create a new Web Cloud Service
Rename the WebRole to NorthWind_WebRole by clicking the pencil icon.
Click the OK button to create the solution.
When the project template has finished creating items you should be presented with the Default.aspx page. If not open the Default.aspx page.
Ensure that you are viewing the Default.aspx page in Design View by clicking the Design button
Drag and drop a GridView control from the Data section of the Toolbox onto the design canvas.
The steps in detail follow below
Creating a new cloud project
Purpose of Screen: Create a New Cloud Project |
To create a cloud service application
Purpose of Screen: To create a cloud service application |
Creating a new web role
You will need to click on the pencil to rename the web role.
Purpose of Screen: Add an ASP.NET Web Role |
Northwind_WebRole
See previous step about click the pencil.
Purpose of Screen: Naming our Web Role |
Adding a GridView control
Purpose of Screen: To add a GridView control |
Select <New data source..>
Purpose of Screen: To bind a data source to our grid |
Choosing a data source type
We are selecting our Northwind SQL Server database.
Purpose of Screen: Choosing a data source type |
Choose Your Data Connection
Purpose of Screen: Choose Your Data Connection |
Save the connection string
Purpose of Screen: Save the connection string |
Create a "select" SQL statement
This is the data that will show up in our GridView control.
Purpose of Screen: Create a "select" SQL statement |
Build the select statement
It will be used to populate our GridView.
Purpose of Screen: Build the select statement |
The test query
You can test the query before even running it.
Purpose of Screen: The test query |
Run the application to test the grid and the database
We will now run our application.
Purpose of Screen: Run the application to test the grid and the database |
Our cloud application running on the local developer cloud
The next step is run the application in the cloud.
Purpose of Screen: Our cloud application running on the local developer cloud |
So now we are moving to the cloud and our goal is to deploy our database in SQL Azure. We have a few steps to get this done.
Login into https://windows.azure.com
Note that when you create a server (from the SQL Azure tab), you will need to specify:
- Administrator UserName
- Administrator Password
- Where you want your data geolocated
You will need these later when you modify web.config to support connectivity to SQL Azure from your Windows Azure ASP.NET web application.
Purpose of Screen: Creating our first SQL Azure |
Once you’ve created your server, you will need to create a database. Our database will be called “NorthwindInCloud.” The database will be empty initially, until we add our tables and data.
Server Name established
Note that in this case, the server name has been created as
Purpose of Screen: Click "Create Database" to create our database |
Naming and Sizing for the database
You will now provide:
- Name of your database
- Size for your database (1gb versus 10gb)
- Affects your monthly charges
Purpose of Screen: Provide a name and size |
Recap – Where we are so far
At this point we’ve accomplished quite a bit. We have:
- A server name to connect to
- A database name
- A location
- A size
Purpose of Screen: The next step is the configure firewall settings |
The “Connection Strings” button
I found this button to be “almost true,” in that I got an error for “Encrypt=True.” More on that later.
Purpose of Screen: You will edit these settings |
Unless you modify Firewall settings, you will get connnection failures.
Configuring the Firewall
Purpose of Screen: You will need to configure the firewall to continue |
You will need to add a firewall rule. The good news is that you are told about the IP address, “Your IP address: 205.248.102.81”
Add Firewall Rule
I’m working in Peets Coffee today when I’m writing this post. And I’ve had to add additional firewall rules based on where I’m connecting from.
Purpose of Screen: Make sure you are providing the correct IP address |
Example of a Firewall rule
Depending on your network scenario, you may need to add additional rules.
Purpose of Screen: You may need to add additonal address per your business |
Testing Connectivity
You may need to wait a few minutes for the firewall rules to take hold. Nothing beats a green “Successfully connected to the database” message.
Purpose of Screen: You will need to "Test Connectivity" |
Migrating our data
Now that we have the on-premise database running correctly, we are now ready to deploy to the cloud. To help with this effort we will use the SQL Azure Migration Wizard available on CodePlex.
Purpose of Screen: Download the SQL Azure Migration Wizard |
WinRar to install our SQL Azure Migration Wizard
There’s nothing complex about the installation. Just extract to a holder and you are good to go.
Purpose of Screen: You will need to unzip the files |
Starting the migration wizard
I will select “Analyze and Migrate” to start the process.
Logging in to our local instance of SQL Server
Purpose of Screen: This is to log into your local, on-premise SQL Server |
Selecting the database we wish to upload to SQL Azure
We wish to upload “Northwind”
Purpose of Screen: Select the database that you wish to migrate to the cloud |
Customers Table
We are interested just in the Customer table.
Purpose of Screen: Select the tables you wish to migrate to SQL Azure |
Summary Screen
This allows us to verify our migration.
Purpose of Screen: Summary Screen |
Results Summary
Note the DMO queries that will build the customers table
Purpose of Screen: Code to create the tables and bulk upload data |
Logging in to SQL Azure
Our database up in the cloud (previously created)
Purpose of Screen: NorthwindInCloud is the SQL Azure database where we wish to create the customers table and upload data |
It is time to execute
Purpose of Screen: The magic moment where the migration will happen |
Execution in progress
Purpose of Screen: The migration in progress |
Success !
Purpose of Screen: Success |
Modify web.config
Purpose of Screen: Modify Web.config and change the connection string to point to the cloud |
Make changes to reflect your connection string
Purpose of Screen: Note we commented out the old one |
Running our application as a final test of success
Purpose of Screen: Test the database by running our ASP.NET Cloud application |
Don Quixote would say, “The proof of the pudding is the eating."
Purpose of Screen: The data below is from SQL Azure. Our steps were a success |
Done !