https://docs.microsoft.com/en-us/azure/sql-database/sql-database-connect-query-dotnet-visual-studio
This quickstart demonstrates how to use the .NET framework to create a C# program with Visual Studio to connect to an Azure SQL database and use Transact-SQL statements to query data.
Prerequisites
To complete this quickstart, make sure you have the following:
An Azure SQL database. You can use one of these techniques to create a database:
A server-level firewall rule for the public IP address of the computer you use for this quickstart.
An installation of Visual Studio Community 2017, Visual Studio Professional 2017, or Visual Studio Enterprise 2017.
SQL server connection information
Get the connection information needed to connect to the Azure SQL database. You will need the fully qualified server name, database name, and login information in the next procedures.
Log in to the Azure portal.
Select SQL Databases from the left-hand menu, and click your database on the SQL databases page.
On the Overview page for your database, review the fully qualified server name as shown in the following image. You can hover over the server name to bring up the Click to copy option.
If you forget your server login information, navigate to the SQL Database server page to view the server admin name. If necessary, reset the password.
Important
You must have a firewall rule in place for the public IP address of the computer on which you perform this tutorial. If you are on a different computer or have a different public IP address, create a server-level firewall rule using the Azure portal.
Create a new Visual Studio project
In Visual Studio, choose File, New, Project.
In the New Project dialog, and expand Visual C#.
Select Console App and enter sqltest for the project name.
Click OK to create and open the new project in Visual Studio
In Solution Explorer, right-click sqltest and click Manage NuGet Packages.
On the Browse, search for
System.Data.SqlClient
and, when found, select it.In the System.Data.SqlClient page, click Install.
When the install completes, review the changes and then click OK to close the Preview window.
If a License Acceptance window appears, click I Accept.
Insert code to query SQL database
Switch to (or open if necessary) Program.cs
Replace the contents of Program.cs with the following code and add the appropriate values for your server, database, user, and password.
using System; using System.Data.SqlClient; using System.Text; namespace sqltest { class Program { static void Main(string[] args) { try { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "your_server.database.windows.net"; builder.UserID = "your_user"; builder.Password = "your_password"; builder.InitialCatalog = "your_database"; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { Console.WriteLine("\nQuery data example:"); Console.WriteLine("=========================================\n"); connection.Open(); StringBuilder sb = new StringBuilder(); sb.Append("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName "); sb.Append("FROM [SalesLT].[ProductCategory] pc "); sb.Append("JOIN [SalesLT].[Product] p "); sb.Append("ON pc.productcategoryid = p.productcategoryid;"); String sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1)); } } } } } catch (SqlException e) { Console.WriteLine(e.ToString()); } Console.ReadLine(); } } }
Run the code
Press F5 to run the application.
Verify that the top 20 rows are returned and then close the application window.
Next steps
Learn how to connect and query an Azure SQL database using .NET core on Windows/Linux/macOS.
Learn about Getting started with .NET Core on Windows/Linux/macOS using the command line.
Learn how to Design your first Azure SQL database using SSMS or Design your first Azure SQL database using .NET.
For more information about .NET, see .NET documentation.
Retry logic example: Connect resiliently to SQL with ADO.NET
Another example:
SQL Azure(八) 使用Visual Studio 2010开发应用连接SQL Azure云端数据库
http://www.cnblogs.com/threestone/archive/2012/02/02/2334949.html