Using ADO.NET to connect to a database in Microsoft SQL Azure Database is very similar to connecting to an instance of SQL Server on your premises. This topic describes a few considerations for connecting to SQL Azure and provides an example console application that you can use to get started. For more general information about connecting to a data source with ADO.NET, see Connecting to a Data Source (ADO.NET).
When connecting to a database in SQL Azure with an ADO.NET application, consider the following points:
-
Avoid injection attacks by using the SqlConnectionStringBuilder class. It is available from the .NET Framework to simplify creating the connection string. For more information, see Connection String Builders (ADO.NET).
-
Carefully protect your connection string. A connection string presents a potential vulnerability if it is not secured. For more information, see Protecting Connection Information (ADO.NET).
-
In order to completely secure your connection, especially when connecting to SQL Azure over the Internet, insure that the ADO.NET Encrypt and TrustServerCertificate connection parameters are set. Set the Encrypt connection property to True (Encrypt = True) and the TrustServerCertificate connection property to False (TrustServerCertificate = False). This will insure that the connection is encrypted and any man-in-the-middle attacks are not possible.
-
For general considerations about connecting to databases in SQL Azure, see Connecting to the Server.
Console Application Example
The following C# example is a console application that connects to SQL Azure. The console application performs the following steps:
-
First, the console application uses a
SqlConnectionStringBuilder object to connect to SQL Azure (the master database) and create a database named as specified by the sampleDatabaseName string.
-
Then, the console application uses a second
SqlConnectionStringBuilder object to connect to the new database.
-
Once connected, the console application uses the second
SqlConnectionStringBuilder object to create a database table and perform a number of data manipulation language (DML) statements.
-
Finally, the console application returns data to the console before and after data changes.
Using this Example
To use this example in Visual Studio 2008 with your SQL Azure server, do the following steps:
-
Open Visual Studio and create a new console application.
-
Replace the code in the program file with the code from this example.
-
Replace
<ProvideUserName> with the name of an SQL Azure login that has been assigned the dbmanager role. Note: if you use the login@server username format, the server portion of the name must match the first part of the server's fully qualified name. For example, if your server is servername.database.windows.net, your login name will look like loginname@servername. For more information about SQL Azure roles, see Managing Databases and Logins in SQL Azure.
-
Replace
<ProvidePassword> with the password associated with the login. Note: We recommend using a strong password when creating a login. For more information, see Strong Passwords.
-
Replace
<ProvideServerName> with the fully qualified domain name of your SQL Azure server. For example: servername.database.windows.net
-
Replace
<ProvideDatabaseName> with the name of the database you want the code to create.
-
Run the code.
Note: |
|---|
There are no Transact-SQL statements in this code to remove the new database (named specified by the sampleDatabaseName string). If you run this code more than once without removing the database, an exception may be thrown because it will attempt to create a database that already exists.
|
We do not yet have a code sample for this language.
Concepts
Developer's Guide (SQL Azure Database)