Tuesday 14 February 2012

How to Create a SQL Azure Database

To create a new database, you must connect to the master database. The following steps show you how to create a database using the Management Portal for SQL Azure.
  1. At the top of the navigation pane, expand Subscriptions, expand the subscription, expand the SQL Azure server you created, click master, and then click Manage.
    This opens the Management Portal for SQL Azure logon screen.
  2. Enter the user name and the password that you specified when you created the SQL Azure server, and then click Log on. This opens the Management Portal for SQL Azure in a different browser tab or a new browser window.
    Note: If an error occurs, it displays a message saying "There was an error connecting to the server" beneath the Password textbox. You can click on the message to see the error details. A common error is the firewall rule not created for the client computer.
  3. In the Management Portal for SQL Azure, click New Query.
  4. In the query window, copy and paste the following Transact-SQL script using your own name for the database in place of database_name:
    CREATE DATABASE database_name;
  5. Click Execute. Make sure the result pane showing Command(s) completed successfully.
  6. Click Log off on the upper right corner, and then click Log off again to confirm logging off without saving the query.
  7. Switch back to the Windows Azure Management Portal.
  8. From the View ribbon, click Refresh. The new database is listed in the navigation pane.
SQL Azure Database provides two database editions:
  • Web Edition - Grows up to a size of 5 GB
  • Business Edition - Grows up to a size of 50 GB.
The MAXSIZE is specified when the database is first created and can later be changed using ALTER DATABASE. MAXSIZE provides the ability to limit the size of the database.
You can also use SQL Server Management Studio 2008 R2 (SSMS) to create a SQL Azure database. For a list of supported tools and utilities, see Tools and Utilities Support (SQL Azure Database). For more information on creating SQL Azure database, see How to Create a SQL Azure database.
For each database created on SQL Azure, there are actually three replicas of that database. This is done to ensure high availability. Failover is transparent and part of the service. The Service Level Agreement provides 99.9% uptime for SQL Azure.

Connect to the SQL Azure Database

This section shows how to connect to SQL Azure database using different .NET Framework data providers.
If you choose to use Visual Studio 2010 and your configuration doesn't include a Windows Azure web application as a front-end, there are no additional tools or SDKs needed to be installed on the development computer. You can just start developing your application.
You can use all of the same designer tools in Visual Studio to work with SQL Azure as you can to work with SQL Server. The Server Explorer allows you to view (but not edit) database objects. The Visual Studio Entity Data Model Designer is fully functional and you can use it to create models against SQL Azure for working with Entity Framework.

Using .NET Framework Data Provider for SQL Server

The System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server.
The standard connection string looks like this:
Server=tcp:.database.windows.net;
Database=;
User ID=@;
Password=;
Trusted_Connection=False;
Encrypt=True;
You can use the SQLConnectionStringBuilder class to build a connection string as shown in the following code sample:
SqlConnectionStringBuilder csBuilder;
csBuilder = new SqlConnectionStringBuilder();
csBuilder.DataSource = xxxxxxxxxx.database.windows.net;
csBuilder.InitialCatalog = testDB;
csBuilder.Encrypt = true;
csBuilder.TrustServerCertificate = false;
csBuilder.UserID = MyAdmin;
csBuilder.Password = pass@word1;
If the elements of a connection string are known ahead of time, they can be stored in a configuration file and retrieved at run time to construct a connection string. Here is a sample connection string in configuration file:
<connectionStrings>
  <add name="ConnectionString" 
       connectionString ="Server=tcp:xxxxxxxxxx.database.windows.net;Database=testDB;User ID=MyAdmin@xxxxxxxxxx;Password=pass@word1;Trusted_Connection=False;Encrypt=True;" />
</connectionStrings>
To retrieve the connection string in a configuration file, you use the ConfigurationManager class:
SqlConnectionStringBuilder csBuilder;
csBuilder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
After you have built your connection string, you can use the SQLConnection class to connect the SQL Azure server:
SqlConnection conn = new SqlConnection(csBuilder.ToString());
conn.Open();

No comments:

Post a Comment