LINQ can be used with SQL Azure in the same way it is used with SQL Server. We are going to use the School database again. So to see how we can use SQL Azure with LINQ.
- Create a console appliacation. For the purpose of this walkthrough, I am creating a console appliacation. You can use SQL Azure with LINQ in this way in other managed applications.
- Then right click on the console application project. Select add new item and choose LINQ to SQL Class form the Data tab.
- Go to the Server Explorer and add a new connection. In the Add Connection window provide the information for SQL Azure.
- Now we need to write the usual LINQ query to do a CRUD operation against a database in SQL Azure. Here I am fetching all the records from the Person class.
Program.cs
using System;
using System.Linq;
namespace LINQSQLAzure
{
class Program
{
static void Main(string[] args)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var result = from r in context.Persons select r;
foreach (var r in result)
{
Console.WriteLine(r.LastName + " " + r.FirstName);
}
Console.ReadKey(true);
}
}
}
Expected output,
In this article, I explained LINQ with SQL Azure.