SQL Azure with LINQ



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.

  1. 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.
  2. Then right click on the console application project. Select add new item and choose LINQ to SQL Class form the Data tab.

    SQLAzure1.gif
     
  3. Go to the Server Explorer and add a new connection. In the Add Connection window provide the information for SQL Azure.

      SQLAzure2.gif
     
  4. 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.

    SQLAzure3.gif

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.

Up Next
    Ebook Download
    View all
    Learn
    View all