posted 11/24/2009 by Harish Ranganathan
In the previous post we had examined on getting access to SQL Azure, creating your first database, accessing it with SQL Server Management Studio and then migrating the Northwind database schema to SQL Azure using the SQL Azure Migration Wizard Beta.
As explained earlier, the SQL Azure Migration Wizard migrates the schema of your database after tuning it for working with SQL Azure. However, we would still need to migrate the Data to our SQL Azure Server. At the moment, the step I took was to open the instnwnd.sql script in SQL Server Management Studio (SSMO) and copy the Insert statements alone to execute. Note that, you cannot run all the scripts directly onto the SQL Azure portal like I explained earlier due to the limitations / formats supported currently in SQL Azure.
To begin with, open “C:\SQL Server 2000 Sample Databases” folder (the default location where Northwind and pubs sample database gets installed) and double click on the instnwnd.sql script. It opens up in SSMO. Examine the scripts and navigate to the portion where the INSERT statements begin. I took the portion and put it up as a single insert script. You can download the script from the link below:-
Running the scripts from SQL Server Management Studio
As explained in my previous post, open SSMO and cancel the initial login prompt. Click on “New Query” and specify the server name as the fully qualified Azure Server name i.e. <YOURSERVERNAME>.ctp.database.windows.net, specify the user name, password. Click on “Options” and specify “Northwind” at “Connect to database” option. Click “Connect”. It should be able to connect provided you have specified the path, username, password etc., correctly and created the “Northwind” database as per the previous post. There will be a small error prompt that comes before opening the Query window, as below. You can ignore that and click “ok”
Once you are in the SqlQuery1.sql screen you can check if the database is created and the tables are there by running a bunch of few queries as below:-
SELECT * FROM Products
SELECT * FROM Orders etc.,
You will get blank result sets since we haven't migrated the data yet.
Double lick to open the insertnwnddata.sql script downloaded in the first step. It would open in a new sql window. Copy paste the script into the first window we got once we connected to the Azure Database. If you get a connection error. Try reconnecting using the “New Query” option as explained above. If all is good, you should be able to execute the scripts. Once the scripts are successful, you can test if the data is migrated, by again running the SELECT statements above. They should now list data. If there is an error, try running the individual INSERT statements for each table to verify which table has the issue.
Once this is done, we are all set to bind this data to our ASP.NET Page.
Binding the SQL Azure database to ASP.NET Page
Open Visual Studio 2008 and create a web application project. In the Default.aspx page, drag and drop a GridView control and use the GridView’s wizard to connect to your local Northwind database. Pick the Products table and chose the columns you want to display and complete the steps. You may want to enable paging to avoid all data shown in the same page.
The above steps should add a GridView, a SQL Data Source to the page. Once you run the website, it should display the Products table record with the columns you chose to display. So far, we have bound the local database. We however, intended to bind it to SQL Azure database that we have created.
Binding the GridView to the SQL Azure Database Table
If you thought this is another big process, you might be in for a surprise. Visit the SQL Azure portal (https://sql.azure.com) sign-in with your credentials that you used to create the token/database etc., Click on the “Manage” link that comes in the right of the Configuration Page that you get once you sign-in. The next screen lists all the databases created. Click on the “Northwind” database radio button to select it. Click on “Connection Strings” in the bottom (the one in the top gives the connection string for master, so click on the “Connection Strings” button below the grid that displays the databases. You should get a popup with 3 different connection strings targeting different data access methodologies. Chose the first one (Click on “Copy to Clipboard”) corresponding to the entry for ADO.NET (the first one).
In Visual Studio, open the web.config file of the site that we created earlier and visit the connectionstrings setting. If you had followed the wizard steps, it would have entered a connection string over here, that corresponds to your local database server.
Replace the connection string with the one copied here. Note, you need to update the password that you used instead of “mypassword”. Also, remove the trailing semicolon at the end after password.
If you have done all the steps properly, you should be able to run the solution now and without any other change, the data that is bound to your grid view should display the same as it did when you initially bound it to the local database. You can try paging / sorting etc., to see if all works fine.
"So, practically, we just had to change the connection string to be able to access the database on the SQL Azure Server. From a developer’s perspective, the data access technique, binding, control and configuration remains unchanged.
That’s it for this series. There are quite a lot of steps that I had listed over the last 2 posts. You may want to check every point if you are facing any issue at any stage.
“This article has been re-published from - Original Article”
What kind of email newsletter would you prefer to receive from CodeAsp.Net?