Udostępnij za pośrednictwem


Azure SQL Database: Hello World

In many different forums, I often get ask how one can quickly write a sample application for Azure SQL Database. While there are many different code snippets scattered through the web, one often has to stitch together bits and pieces of code to get a complete solution. If one is not experienced in C# and ADO.NET, this is not always a straightforward task. The objective of this blog is to present a simplified solution to get started with Azure SQL Database. For the solution provided, all one needs to do is simply enter their database credentials, hit F5, and they are programming against their database.

There are, however, a few prerequisites: 1) create an Azure Subscription, 2) provision a SQL Database, 3) configure the firewall, and 4) create a schema and insert sample data. The following video [link] provides a great guide on to accomplish steps 2) and 4) above. For the full getting started guide, please see the following [link].

To get the code, please see the Azure SQL Database: Hello World in the MSDN sample gallery.

Below is the simplified code that will enable one to quickly write a console application to connect to and read from an Azure SQL Database.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data.SqlClient;

namespace HelloWorldAzureSQLDatabase

{

classProgram

{

    static void Main()

    {

SqlConnectionStringBuilder builder = newSqlConnectionStringBuilder();

 

      //The name of the Azure SQL Database server

      //Example: myFirstDatabase.database.windows.net

      builder["Server"] = "<name>.database.windows.net";

 

      //User ID of the entity attempting to connect to the database

      builder["User ID"] = "<userID>";

 

      //The password associated the User ID

      builder["Password"] = "<passowrd>";

 

      //Name of the target database

      builder["Database"] = "<databaseName>";

 

      //Denotes that the User ID and password are specified in the connection

      builder["Integrated Security"] = false;

 

      //By default, Azure SQL Database uses SSL encryption for all data sent between

      //the client and the database

      builder["Encrypt"] = true;

 

      //Create a SqlConnection from the provided connection string

      using (SqlConnection connection = newSqlConnection(builder.ToString()))

      {

        //Begin to formulate the command

        SqlCommand command = newSqlCommand();

        command.Connection = connection;

 

        //Sets the wait time (10 seconds) before terminating the attempt to execute a command and generate an error

        //Default value is 30 seconds;

        command.CommandTimeout = 10;

 

        //Specify the query to be executed

        command.CommandType = System.Data.CommandType.Text;

        command.CommandText = "Select count(*) from <tableName>";

 

        //Open connection to database

        connection.Open();

 

        //Read data from the query

        SqlDataReader reader = command.ExecuteReader();

 

        while (reader.Read())

        {

          //Formatting will depend on the contents of the query

          Console.WriteLine("Value: {0}", reader[0]);

        }

 

      Console.WriteLine("\nPress any key to continue");

      Console.ReadLine();

}

}

}

} 

From the link provided above, there are two quick ways that you can get going with this sample code. First is to simply download the sample code for the MSDN Code Gallery. The other option is to open Visual Studio, click File -> New -> Project. On the left-hand side of the New Project Window, click “Online” -> Samples -> Visual C#. In the search box in the top-right hand corner of the windows, search for “Azure SQL Database: Hello World”. Select the appropriate project and “click” OK.