34
Demo Script Deploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last updated: 6/19/2022 Content ID: SQL10R2AZ00-DEMO-02 Prepared by: Roger Doherty

Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

  • Upload
    votram

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

Demo ScriptDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier ApplicationsLab version: 1.0.0

Last updated: 5/8/2023

Content ID: SQL10R2AZ00-DEMO-02

Prepared by: Roger Doherty

Page 2: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

CONTENTS

OVERVIEW................................................................................................................................................. 4Time Estimates........................................................................................................................................ 4

SETUP AND CONFIGURATION................................................................................................................. 4Task 1 – Installing Prerequisites..........................................................................................................5

Task 2 – Configuring SQL Azure...........................................................................................................5

Task 3 – Configuring Windows Azure..................................................................................................5

Task 4 – Deploying Cloud Service to Windows Azure..........................................................................6

Task 5 – Cleaning Up from a Previous Demo.......................................................................................6

OPENING STATEMENT............................................................................................................................. 7

STEP-BY-STEP WALKTHROUGH.............................................................................................................7Task 1 – Developing a DAC in Visual Studio 2010................................................................................7

Task 2 – Deploying a DAC to SQL Server............................................................................................11

Task 3 – Modifying and Upgrading a DAC..........................................................................................15

Task 4 – Testing the Application On-Premises...................................................................................19

Task 5 – Deploying the Same DAC to SQL Azure................................................................................20

Task 6 – Testing the Application Running in the Cloud......................................................................26

SUMMARY................................................................................................................................................ 27

Page 3: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

Overview

SQL Server 2008 R2 introduced a new paradigm for developing, deploying and managing database applications known as “Data-tier Applications” or DAC for short. DACs provide a single unit of deployment for the data-tier portion of an application. SQL Server 2008 R2 and SQL Azure both support DAC deployment. SQL Server 2008 R2 also supports DAC upgrade, dramatically simplifying the task of upgrading a production database schema. This demo is designed to introduce developers who are new to DAC development, deployment and upgrades to the key value propositions of this important foundational technology.

Note: This demo was built on SQL Server 2008 R2 / Visual Studio 2010 / SQL Azure CU5 bits. At the time of this writing SQL Azure does not support DAC upgrades but support for this is in the near-term roadmap.

This demo uses the following technologies:

Windows Server 2008 R2 / Windows Azure

SQL Server 2008 R2 / SQL Azure

Visual Studio 2010

Windows Azure SDK and Windows Azure Tools for Microsoft Visual Studio

Time Estimates

Estimated time for setting up and configuring the demo: 10 min (if you already have Azure account)

Estimated time to complete the demo: 10 min

Setup and Configuration

Page 4: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

Make sure you have checked all the dependencies for this demo before running the setup.

Task 1 – Installing Prerequisites

1. Install SQL Server 2008 R2

2. Install AdventureWorks 2008 R2 Sample Databases

3. Install Visual Studio 2010 Ultimate

4. Install Windows Azure SDK and Windows Azure Tools for Microsoft Visual Studio

Task 2 – Configuring SQL Azure

1. Purchase a Windows Azure offer that includes SQL Azure.

2. Complete the service activation procedures for your subscription.

3. Navigate to the SQL Azure Developer Portal.

4. Create a SQL Azure logical server. Make a note of the user name and password you selected for your administrative user account.

5. Create a new login called SampleUser with the password SqlAzureRocks2. See the CreateSampleUserSqlAzureLogin.sql script in the assets folder of this demo.

Task 3 – Configuring Windows Azure

1. Navigate to the Windows Azure Management Portal.

2. Provision a new API certificate if you don’t already have one.

3. Provision a new affinity group located in the same data center as your SQL Azure database.

4. Add a new Hosted Service.

5. Choose an available URL similar to FirstSqlAspWebApp.

6. Choose the affinity group you created in step 3.

7. Add a new Storage Account if you do not already have one.

Page 5: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

8. Configure the URL for the public storage account name and make a note of it.

9. Choose the affinity group you created in step 3.

Task 4 – Deploying Cloud Service to Windows Azure

1. Open the FirstSqlAspWebAppAzure.sln solution in the Source\Code folder of this demo.

2. Open web.config.

3. Edit the AventureWorks connection string on line 23. Change the Data Source to the DNS name for your SQL Azure logical server.

4. Right click on the project and select Publish…

5. Choose the Deploy your Windows Azure project to Windows Azure option.

6. Choose the credentials, hosted service and storage account you provisioned above.

7. Click OK

8. Open the Windows Azure Activity Log window.

9. Wait for the deployment to complete (about 15 minutes).

10. Click on the DNS name url and test to make sure the application is functioning.

11. Close the browser window that you just launched.

12. Leave the Visual Studio window above open to use in your demo.

Task 5 – Cleaning Up from a Previous Demo

1. Delete the FirstSqlAspWebAppDb Data-tier Application from your local SQL Server instance and SQL Azure

2. Delete any old versions of the FirstSqlAspWebAppDb database that were created during DAC upgrade on both your local SQL Server instance and SQL Azure.

Page 6: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

Opening Statement

In this demo, I will introduce you to Data-tier applications, also known as DAC’s. DAC’s are an exciting new capability introduced in SQL Server 2008 R2 that provide a single unit of deployment for database applications. We’ll be showing three basic things:

First you will see how easy it is to develop and deploy DAC’s with Visual Studio 2010 in comparison to traditional script-based approaches

Second you will see how DAC upgrades dramatically simplify the maintenance of database applications

Third you will see how you can deploy the same DAC’s developed for SQL Server 2008 R2 to SQL Azure

Step-by-Step Walkthrough

Task 1 – Developing a DAC in Visual Studio 2010

Action Script Screenshot

Page 7: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

1. Launch Visual Studio 2010

2. Open the FirstSqlAspWebAppDb.sln solution found in the Code \ Begin folder of this demo.If Database Schema View is not visible, display it using View > Database Schema View

3. Expand the Schemas > Production > Tables node

4. Expand the Product node

5. Expand the Keys node

6. Expand the Indexes node

Let us start with an existing DAC project that I imported from an existing database using the DAC import wizard.

Database Schema View is an offline representation of a database.

The main table in our sample is Product.

Notice the DAC import wizard also imported keys and indexes

7. Open the create table script for Product by double-clicking the Schemas > Production > Tables > Product node in Database Schema View

8. Scroll to the bottom where the constraints are created

9. Close Product.table.sql window

Let us check out the script generated for the product table.

Note that all of the constraints are created in line with the CREATE TABLE statement

This is by design since DAC projects do not support the ALTER TABLE command

Page 8: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

10. In Database Schema View, right click on the Schemas > Production > Tables node

11. Select Add > Table…

12. Enter ProductSettings in the Name field

13. Click Add

14. Edit the script by replacing the placeholder columns with two real columns (Name, Value) consistent with the code in Snippets.txt located in the Source\Assets folder for this demo

Let us create a new table called ProductSettings.

I am going to introduce an error by misspelling the nvarchar data type.

15. Press F6 to build the solution

16. Display the Error List window

17. Edit the script and correct the error by changing nvarchars to nvarchar consistent with the code in Snippets.txt located in the Source\Assets folder for this demo

18. Press F6 to build the solution

Let us see what happens when we do a build.

We get an unresolved reference error for data type nvarchars

This error was caught without even connecting to SQL Server because of the new shared Transact-SQL Language Service component used by VS and other tools

This way many common errors can be resolved before the database is even deployed

Page 9: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

19. In Database Schema View, right click on the Schemas > Production > Programmability > Stored Procedures node

20. Select Add > Stored Procedure

21. Enter GetProductSettings in the Name field

22. Click Add

23. Edit the script. Delete the template parameters and select statement. Add a select statement using Intellisense consistent with the code in Snippets.txt located in the Source\Assets folder for this demo

Let us add a stored procedure that queries our new table.

Notice I get full Intellisense against database objects even though I’m in a disconnected state...

24. In Solution Explorer, right click the Project and select Properties

25. Display the Code Analysis tab

26. Enable the Enable Code Analysis on Build option

27. Expand the Microsoft.Rules.Data.Design node

28. Expand the Microsoft.Rules.Data.Naming node

Static code analysis is another benefit of this new offline development experience

It is turned off by default so let us go ahead and enable it

Here are some of the standard rules governing design, naming and performance

The list is extensible with your own

Page 10: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

29. Expand the Microsoft.Rules.Data.Performance node

rules

30. Press F6 to rebuild the solution.

31. Display the Error List Window

32. Scroll to the bottom

33. Double click on Warning 30

34. In Database Schema View, right click on the Schemas > Production > Programmability > Stored Procedures > GetProductSettings node.

35. Select Refactor > Expand Wildcards

36. Click on the same node and select Refactor > Fully-qualify Names

37. Press F6 to rebuild the solution

Let us rebuild and see what we get.

I have several warnings here but let us examine the one that applies to the code I just wrote.

Or course it is not a good idea to use SELECT * in a stored procedure because code that calls it can break if the underlying table structure changes

Let’s use refactoring support to expand the wildcards

While we’re at it, let’s also use refactoring to fully qualify the table names to avoid default schema related errors

This time when we rebuild the warning is gone

Task 2 – Deploying a DAC to SQL Server

Action Script Screenshot

Page 11: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

38. Switch to SQL Server Management Studio

39. Expand the Management node

40. Highlight the Data-tier Applications node

41. Right click on the Management > Data-tier Applications node

42. Select Deploy Data-tier Application…

43. Click Next

Now that my DAC project is ready to deploy let me switch hats and assume the role of a DBA.

Registered DAC’s are displayed in a new Object Explorer node. Right now, no DAC’s are registered.

Next we’ll launch the DAC deployment wizard

44. Click the Browse button

45. Navigate to the obj\debug folder beneath the solution you created in the previous segment.

46. Select the file named FirstSqlAspWebAppDb.dacpac

47. Click Open

48. Click Next

When you build a DAC project, it produces a file with a .dacpac extension.

This is essentially a zip file that contains all of the metadata and scripts required to deploy a DAC.

Let’s locate the one for our sample database that we just built in Visual Studio

Page 12: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

49. This will display the Update Configuration step

50. Click Next

At this point I just have to set the name of the deployed DAC and database and path to the data and log files

We will go with the defaults.

51. This will display the Summary step

52. Click Next

Page 13: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

53. This will display the Deploy DAC step

54. Click Finish

The wizard reports the status of the deployment operation and we are done!

55. In Object Explorer, highlight the Management > Data-tier Applications > FirstSqlAspWebAppDb node

56. Right-click on the Databases node

57. Select Refresh

58. Highlight the Databases > FirstSqlAspWebAppDb node

Our new DAC shows up in Data-tier applications, and the associated database shows up in Databases

Page 14: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

59. Open a command prompt

60. Set the current directory to the Source\Assets directory for this demo.

61. Execute the OnPremBcp.cmd script

Let us quickly run a script that uses BCP to load our sample database with some data.

62. Switch back to SQL Server Management Studio

63. Right-click on Databases > FirstSqlAspWebAppDb > Tables > Production.ProductSettings

64. Select Edit Top 200 Rows

65. Add two new rows with the following name value pairs: Color / Green, Size / Large

66. Close the edit window.

Let us add a couple of rows to the table we created.

Task 3 – Modifying and Upgrading a DAC

Page 15: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

Action Script Screenshot

67. Switch back to Visual Studio

68. In Database Schema View, double-click on the Schemas > Production > Tables > Product Settings node

69. Add a new column between Name and Value called Created consistent with the code in Snippets.txt located in the Source\Assets folder for this demo.

When developers hand off database upgrades to DBA’s, the process can be difficult and time consuming.

Typically, it involves the development of specialized upgrade scripts.

Next, I will show you how DAC’s can dramatically simplify this process.

Let us switch back to the role of a developer and add a new column to track the creation date for a row.

Note that I am adding it in the middle.

Page 16: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

70. In Solution Explorer, right-click on the project and select Properties

71. Display the Project Settings tab

72. Change the Version to 1.0.0.1

73. Rebuild the solution.

Let us increment the build number and rebuild the solution.

Page 17: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

74. Switch to SQL Server Management Studio

75. Right-click on the Management > Data-tier Applications > FirstSqlAspWebAppDb

76. Select Upgrade Data-tier Application

77. Click Next

Normally at this point, the developer would hand off a bunch of upgrade scripts to the DBA that may or may not be tested.

With DAC projects, all the developer has to do is hand off a single .dacpac file to the DBA.

It is up to SQL Server to determine how to upgrade the existing DAC to the new version, not the DBA.

78. This will display the Select Package step

79. Click Browse

80. Navigate to the obj\Debug folder beneath the FirstSqlAspWebAppDb DAC project

81. Select First SqlAspWebAppDb.dacpac

82. Click Open

83. Click Next

Let us select the new version of the dacpac file.

Page 18: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

84. This will display the Detect Change step.

85. Enable the Proceed despite possible loss of changes option.

86. Click Next.

The upgrade wizard has detected some drift in the database.

This can occur if changes are made directly to a database schema without upgrading a DAC.

In this case we are dealing with a false positive, so I will ignore the warning and continue.

87. This will display the Summary step.

88. Click Next.

Page 19: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

89. This will display the Upgrade DAC step.

90. Click Finish.

First, the wizard creates a new database.

Next, it sets the existing database to read-only and renames it.

Then, the wizard copies data from the old database to the new database.

Finally, it renames the new database to the original name.

This is called a “Side-by-Side” upgrade scenario.

DAC will support “in-place” upgrade scenarios in the future.

91. Right-click on the Databases > FirstSqlAspWebAppDb > Tables > Production.ProductSettings node.

92. Select Select Top 1000 Rows.

93. Exit Visual Studio.

Here we can see the effect of the DAC upgrade operation on the ProductSettings table.

The existing data was preserved and the new column was added in the right order.

DAC upgrade automatically handles many complex dependency issues and orders the upgrade operations correctly.

This frees up the DBA to focus on other issues.

Page 20: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

Task 4 – Testing the Application On-Premises

Action Script Screenshot

94. Locate the FirstSqlAspWebApp folder in the Code folder of this demo.

95. Double-click on FirstSqlAspWebApp.sln

96. In Solution Explorer, double-click on Web.config

97. Highlight the Initial Catalog setting in the AdventureWorks connection string.

98. Press F5 to start debugging.

99. Add, edit and delete a product. Make sure to use a ListPrice > 3500.

100. Close the debug browser window.

At this point, we have deployed our DAC and populated it with some data.

Let us test our sample application to make sure it is working.

As you can see our application is already configured to connect to the sample database deployed using a DAC.

Let us debug the application.

Our sample application is working as expected, listing products from our new database.

It’s just a simple ASP.NET web application that shows how to do CRUD operations against SQL Server.

Task 5 – Deploying the Same DAC to SQL Azure

Action Script Screenshot

Page 21: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

101. Switch to SQL Server Management Studio.

102. In Object Explorer, click Connect > Database Engine.

103. Enter SqlAzureTrainingKitAlias in the Server name field.

104. Set Authentication to SQL Server Authentication.

105. Specify the name and password of the administrative user you configured when you initially provisioned your SQL Azure logical server.

106. Click Connect.

107. Expand the SqlAzureTrainingKitAlias > Management node

108. Right-click on Data-tier Applications

109. Select Deploy Data-tier Application…

Now I will show you how DAC projects make it easy to deploy the exact same sample application to the cloud.

I have previously provisioned a SQL Azure logical server using my Windows Azure subscription.

Let us connect to our SQL Azure logical server from SQL Server Management Studio.

Now we will deploy the same Data-tier application to SQL Azure.

Page 22: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

110. This will launch the Deploy Data-tier Application wizard.

111. Click Next.

112. Click Browse.

113. Locate the FirstSqlAspWebAppDb.dacpac file in the obj\Debug folder of the Visual Studio FirstSqlAspWebAppDb project.

114. Click Open.

115. Click Next.

Let us locate the same DAC.

Page 23: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

116. This will display the Update Configuration step.

117. Click Next.

Let us go with the default database name.

118. This will display the Summary step.

119. Click Next.

Page 24: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

120. This will display the Deploy DAC step.

121. Click Finish.

Note that everything worked except for the step that required deploying my Windows login to SQL Azure.

SQL Azure only supports SQL Server Authentication.

Page 25: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

122. In Object Explorer, highlight the SqlAzureTrainingKitAlias > Management > Data-tier Applications > FirstSqlAspWebAppDb node

123. Also highlight the SqlAzureTrainingKitAlias > Databases > FirstSqlAspWebAppDb node

124. Open a command prompt

125. Set the current directory to the Source\Assets directory for this demo.

126. Execute the CloudBcp.cmd script.

127. Exit the command prompt.

Our DAC is now registered in SQL Azure and a new database was created.

Let us copy some data into it.

128. In Object Explorer, right Click the SqlAzureTrainingKitAlias > Databases > FirstSqlAspWebAppDb node

129. Select New Query

130. Enter a query against the Production.Product table consistent with the code in Snippets.txt located in the Source\Assets folder for this

Let us write a query to verify there is some data in our SQL Azure database.

Page 26: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

demo.

131. Press F5 to execute the query.

132. Close the query window.

133. In Object Explorer, right Click the SqlAzureTrainingKitAlias > Databases > FirstSqlAspWebAppDb node

134. Select New Query

135. Create a new user named SampleUser and grant privileges to the Production schema consistent with the code in Snippets.txt located in the Source\Assets folder for this demo.

136. Press F5 to execute the query.

137. Close the query window.

We need to provision a user in the SQL Azure database and grant some privileges.

Task 6 – Testing the Application Running in the Cloud

Action Script Screenshot

Page 27: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

138. Switch to the Visual Studio 2010 window with the FirstSqlAspWebAppAzure solution open that you deployed during setup and configuration.

139. Highlight line 23 in web.config.

Now we will test the sample application running in the cloud.

This is the same exact application we saw before running on-premises.

The only difference is that the connection string in web.config points to my newly deployed DAC in SQL Azure.

I previously published my cloud solution to Windows Azure, and you can see the deployment was successful.

Page 28: Deploying and Managing SQL Azure Databases with …€¦ · Web viewDeploying and Managing SQL Azure Databases with Visual Studio 2010 Data-tier Applications Lab version: 1.0.0 Last

140. Click on the link specified in Website URL.

141. Add, edit and delete a product.

Now I will navigate to the URL for the deployed Windows Azure application.

The application looks identical, and I can perform all the same operations I could in the on-premises deployment of the application.

Summary

In this demo, you saw how easy it is to develop and deploy a Data-tier Application. You also saw how Data-tier applications can dramatically simplify the maintenance of database applications when new versions of your schema need to be deployed. Finally, you saw how you can leverage the same Data-tier application projects to develop, deploy and maintain both on-premises SQL Server based solutions and SQL Azure solutions running in the cloud.