Exercise 3:Build a Windows Azure Application that Accesses SQL Azure
หน้าแรก windows azure Exercise 3:Build a Windows Azure Application that Accesses SQL Azure
In this exercise, we will create a simple Windows Azure application to allow us to manipulate the data in the Customer table of the AdventureWorksLT2008 database.
The purpose of this exercise is to demonstrate just how simple it is to work with SQL Azure and Windows Azure using the graphical Visual Studio 'drag and drop' approaches.
There are certain system level features of SQL Server that tools like Visual Studio depend on for the graphical developer features, which are currently not supported in SQL Azure. What we will do instead is create our application against our local instance of SQL Server 2008. Then we will be able to simply change the connection string at release time. This is a useful pattern for developing Windows Azure applications that rely on SQL Azure.
Task 1 Load Sample Database to your on premise SQL Database
- Open a new instance of SQL Server Management studio, and connect to your local instance SQL Server.
- Verify that you have the AdventureWorksLT2008 database attached.
AdventureWorksLT2008 can be downloaded from here: http://www.codeplex.com/MSFTDBProdSamples
Task 2 Load Sample Database to your SQL Azure HolTestDB
- In the Object Explorer right click on HolTestDB and create a new query.
- Paste in to the query window the SQL found in the Assets folder.
Note:This script contains a cleaned up export script from the AdventureWorksLT2008 sample database. Future versions of SQL Server Management Studio will have specific support for SQL Azure avoiding the need to clean up the exported scripts. - Execute the query. This may take some time, as you are creating a subset of the Adventure Works database.
- Close the query window.
Task 3 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 elevated as Administrator, from Start | All Programs | Microsoft Visual Studio 2008
right-clickMicrosoft Visual Studio 2008 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 using the configuration settings from the screenshot below.

Figure 1 Creating a new Web Cloud Service
- Now Select the appropriate(VB or C#) ASP .NET Web Role from the list in the dialog and click the arrowto add it to your solution.

Figure 2 Adding a Web Role to the Solution
- Rename the WebRole to AdventureWorksWeb 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 Datasection of the Toolbox onto the design canvas.

Figure 3 Adding a Grid View
Note:Normally we would simply configure our datasource at this point using the Wizard. However, the Add Connection dialog in Visual Studio does not currently work in this ctp, so for this exercise we will need to manually create a connection string first. To simplify debugging we will start by testing against our local SQL Server 2008 instance before changing our connection string to point to SQL Azure. - From the SmartTagon the GridView you just created choose New data source.

Figure 4 Using the Smart Tag
- In the Wizard choose a Data Source Type of Database and leave the default ID.

Figure 5 Choosing a Data Source
- Click OK
- In the Configure Data Source Wizard, click New Connection.

Figure 6 Creating a new Connection
- In the Add Connectiondialog configure a connection to your local copy of AdventureWorksLT

Figure 7 Configuring a connection to the local server
- Press Test Connection you should receive a dialog indicating success

Figure 8 Confirmation of a successful connection
- Click OK to close the Add Connectiondialog box.
- Click Next to continue through the Wizard
- Ensure that the Yes, save this connection as is checked and use the nameAdventureWorksLTConnectionString

Figure 9 Saving the connection string
Note:By saving the connection string to the application configuration file we can easily return later and use the SQL Azure connection string instead. - Click Next
- Choose the table named Customer and check the FirstName, LastName, CompanyName aEmailAddress.

Figure 10 Choosing query columns
- Select the SELECT statement at the bottom of the dialog and press Ctrl-C to copy to the clipboard.
- Click the radio button to Specify a custom SQL statement of stored procedureWe need to do this because AdventureWorks uses a named Schema (SalesLT) that we need to explicitly reference.
- Click Next button
- Paste the statement you copied into the SQL Statement box.

Figure 11 Pasting the SQL string
- Change the statement to read
SELECT [FirstName], [LastName], [CompanyName], [EmailAddress] FROM [SalesLT].[Customer]
- Press Next
- Press Test Query and you should see results returned.

Figure 12 Testing the query
- Click Finish
- Press F5 to run the application in the Azure Development Fabric
- The application will execute and you will see the list of all customers in the browser

Figure 13 Running against the local SQL Server instance
- Close the browser and return to Visual Studio
- Open the web.config file
- Find the connectionStringssection and modify the connectionString to the following.
"AdventureWorksLTConnectionString" connectionString="Initial Catalog=HoLTestDB;Data Source=REPLACE_SERVER_NAME.database.windows.net;encrypt=true;User ID=HoLTestUser;Password=REPLACE_HoLTestUser_PASSWORD;TrustServerCertificate=true;" providerName="System.Data.SqlClient" />
Note:You will need to update this string with your SQL Azure server name, and the credentials of the HolTestUser you created in previous exercises. - Press F5 to run the application once again. This time the list of Customers is being retrieved from SQL Azure
ขึ้นไปด้านบน
