Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Connect to Azure SQL Database Through ASP.NET

Updated: July 15, 2015

You can bind data from Microsoft Azure SQL Database to ASP.NET controls just as you would with SQL Server. The SqlDataSource class is compatible with Microsoft Azure SQL Database, so you can convert your existing solutions with only a change to the connection string.

For general considerations about connecting to databases in Azure SQL Database, see Guidelines for Connecting to Azure SQL Database Programmatically.

This example shows how to bind the ASP.NET GridView control to the Azure SQL Database from a service hosted in Azure. By using the GridView control, you can insert, update, and delete data from a Azure SQL Database without writing any code.

  1. The Storage Emulator installed with the Azure SDK must be initialized to point to the correct database, as described in Initialize the Storage Emulator by Using the Command-Line Tool.

  2. Review the information in Guidelines for Connecting to Azure SQL Database Programmatically to ensure your firewall and ports have been configured to connect with Azure SQL Database.

  1. Connect to the Azure SQL Database server by using the sqlcmd utility. For information about connecting, see How to: Connect to Azure SQL Database by using sqlcmd.

  2. Run the following Transact-SQL commands:

    2> GO
    1> QUIT
  3. Connect to TestDb and run the following Transact-SQL commands:

    1> CREATE TABLE Table1 (Col1 int primary key, Col2 varchar(20));
    2> GO
    1> INSERT INTO Table1 (Col1, Col2) VALUES (1, 'string1'), (2, 'string2');
    2> GO
    1> QUIT

  1. In Visual Studio, on the File menu, click New, and then select Project.

    You must run Visual Studio as an administrator to use the Azure Development Fabric.

  2. In the New Project dialog box, in the Installed templates list, select Cloud for either Visual C# or Visual Basic.

  3. In the Templates list, select ASP.NET Web Application. Type DataBinding for the name and then click OK.

  4. In the New ASP.NET Project dialog box, select Web Forms from the list of templates and ensure that the Host in the cloud box is checked. Click OK.

  5. In Solution Explorer, double-click Default.aspx, and then click Design to switch to the design view.

  6. Drag the GridView control from the Toolbox to the design surface.

  7. In the Properties window for the GridView, set AutoGenerateDeleteButton and AutoGenerateEditButton to true.

  1. In Solution Explorer, double-click Web.config.

  2. In Web.config, replace the current connection string with the following, and provide the appropriate values:

  3. In Default.aspx, click Source to switch to the source view.

  4. Add the following markup inside the <asp:Content> tag to create a SqlDataSource control for your connection:

    This includes the SELECT, INSERT, UPDATE, and DELETE logic for the control.

  5. Add the following attributes to the markup of your GridView control in Default.aspx: DataSourceID="SqlAzureDataSource"DataKeyNames="Col1".

  6. Press F5 to run the solution. The browser opens and shows the contents of the table, and enables the user to edit and delete the values in the table.

See Also

© 2015 Microsoft