CHAPTER 8 ■ SQL AZURE 253 Run the SQL script in Listing 8-5 to insert data into these two tables and run the script in Listing 8-6. You will see that the correct data is returned. Listing 8-5. Sample Data to Insert into the Tables Created Previously INSERT INTO UserTable ([Password], FirstName, LastName) VALUES ('password', 'Henry', 'Li') GO INSERT INTO UserTable ([Password], FirstName, LastName) VALUES ('password', 'Emma', 'Li') GO INSERT INTO UserTable ([Password], FirstName, LastName) VALUES ('password', 'David', 'Kruger') GO INSERT INTO Address( UserID, Address1, City, [State], Zip, County, Email1, Email2 ) VALUES( 1, '12 King Street', 'Salem', 'OR', '97304', CHAPTER 8 ■ SQL AZURE 254 'Polk', 'yinghong@softnetsolution.net', 'henry@softnetsolution.net') GO INSERT INTO Address( UserID, Address1, City, [State], Zip, County, Email1, Email2 ) VALUES( 3, '99 Universal Park', 'Denver', 'CO', '80201', 'Denver', 'david.kruger@example.com', ' ') GO INSERT INTO Address( UserID, Address1, City, [State], Zip, County, Email1, Email2 ) VALUES( 2, '19 West Ave', 'Aberdeen', 'WA', '98520', 'Grays Harbor', 'emma@example.com', ' ') GO CHAPTER 8 ■ SQL AZURE 255 Listing 8-6. Query Data with Joined Tables SELECT U.FirstName, U.LastName, A.Address1, A.City, A.Email1 FROM UserTable U JOIN Address A ON A.UserID =U.UserID WHERE U.LastName = 'Li' GO SELECT * FROM UserTable SELECT * FROM [Address] GO Now try to delete a record from UserTable using the highlighted code in Figure 8-11. An expected SQL exception will be thrown because the record is referenced by Address. This demonstrates that SQL Azure is truly a relational database service. Figure 8-11. An expected SQL exception is thrown when trying to delete a record from a referenced table Connect to a SQL Azure Database Using ADO.NET To connect to SQL Azure using ADO.NET is very similar to connecting to a traditional database. The difference is the connection string format. Let's take an example where there are two identical databases, with one from SQL Azure and the other from the local workstation. Listing 8-7 shows the difference between the two connection strings used to connect to these two databases. Listing 8-7. Comparison of Connection Strings Between SQL Azure and Local SQL Database "Server=tcp:{SQL Azure Server Name}.ctp.database.windows.net;Database={SQL Azure Database};User ID={my user ID};Password={my password};Trusted Connection=False;" "Data Source={Local Database Workstation};Initial Catalog={Database Name};Integrated Security=True;" An example of how to connect to SQL Azure from C# code will be presented in a SQL Azure access tool, SQLAzureConnect, developed later in this chapter. CHAPTER 8 ■ SQL AZURE 256 Migrate Existing Databases from an On-Premises System to SQL Azure Existing on-premises databases can easily migrate to SQL Azure. In the first release of SQL Azure, the scripts generated by SQL Server Management Studio need some extra cleanup. This can be avoided in future SQL Azure releases. In this section we are going to use SQL Server Management Studio to generate SQL scripts and migrate an existing database from a local database to a SQL Azure database. The database we'll use is SQLAzure, which you can create on a local workstation using Listing 8-1 and Listing 8-3. The steps are as follows. 1. Open SQL Server Management Studio, right-click on the database node SQLAzure in the object browser pane, and select Tasks h Generate Script. 2. Select the database SQLAzure (as Figure 8-12 shows) and check all objects from the dialog boxes. Figure 8-12. Select database SQLAzure 3. Before moving forward, some options need to be set correctly as Figure 8-13 shows. • Convert UDDTs to Base Type: This option needs to be set to true since SQL Azure does not support user-defined types. They need to be converted into underlying SQL Azure portable types. CHAPTER 8 ■ SQL AZURE 257 • Script extended properties: This option needs to be set to false since SQL Azure does not support extended properties. • Script USE DATABASE: This option needs to be set to false since SQL Azure does not support the USE statement. • Script Data: This option needs to be set to false since we do not care about the data at this moment. Figure 8-13. Set the values to false for those options that SQL Azure does not support . Between SQL Azure and Local SQL Database "Server=tcp:{SQL Azure Server Name}.ctp.database .windows. net;Database={SQL Azure Database};User ID={my user ID};Password={my password};Trusted Connection=False;"