Migrate to Innovate Summit:
Learn how migrating and modernizing to Azure can boost your business's performance, resilience, and security, enabling you to fully embrace AI.Register now
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Create code to query the database in Azure SQL Database
In Visual Studio, create a new project.
In the New Project dialog, select the C# Console App.
Enter sqltest for the project name, and then select Next.
Select a (Long-term support) Framework option, such as .NET 8.0, and then select Create. The new project is created.
Select Project > Manage NuGet Packages.
In NuGet Package Manager, select the Browse tab, then search for and select Microsoft.Data.SqlClient.
On the Microsoft.Data.SqlClient page, select Install.
If prompted, select OK to continue with the installation.
If a License Acceptance window appears, select I Accept.
When the install completes, you can close NuGet Package Manager.
In the code editor, replace the Program.cs contents with the following code. Replace your values for <your_server>, <your_username>, <password>, and <your_database>.
C#
using System;
using Microsoft.Data.SqlClient;
using System.Text;
namespacesqltest
{
classProgram
{
staticvoidMain(string[] args)
{
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "<your_server>.database.windows.net";
builder.UserID = "<your_username>";
builder.Password = "<password>";
builder.InitialCatalog = "<your_database>";
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
Console.WriteLine("\nQuery data example:");
Console.WriteLine("=========================================\n");
String sql = "SELECT name, collation_name FROM sys.databases";
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
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
To run the app, select Debug > Start Debugging, or select Start on the toolbar, or press F5.
Verify that the database names and collations are returned, and then close the app window.
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.
This article shows you how to use .NET to create a program that connects to a database in Azure SQL Database, or Azure SQL Managed Instance, and queries it using Transact-SQL statements.
Jiachen Jiang (PM, Data Access) will walk us through how to work with SQL Server using C# and .NET and decide which approach works best for your scenario. They explain the differences between the three solutions and demo how to connect to and query a database in each one. Chapters 00:00 - Intro 00:52 - Ways of using SQL with .NET 01:35 - Demo: Connecting to SQL Server with EF Core 03:33 - Demo: SQL Server with Dapper 04:30 - Demo: SQL Server with ADO.NET 04:55 - Summary & Wrap-up Recommended resources
Deploying an ASP.NET web application entails getting the necessary files and resources from the development environment to the production environment. For da... (C#)