Click here to Skip to main content
Click here to Skip to main content
Go to top

Using LINQ to SQL with SQL Server Compact

, 12 May 2009
Rate this:
Please Sign up or sign in to vote.
In this article, I will cover how to access data in SQL Server Compact databases (.sdf files) using the new development technologies such as LINQ.

Introduction

In this article, I will cover how to access data in SQL Server Compact databases (.sdf files) using new development technologies such as LINQ. LINQ is the new initiative of Microsoft to support Object-Relational Mapping concepts and design patterns. LINQ provides full type-safety and compile-time checking of query expressions in order to minimize the object-relational concepts mismatch, and enables managing relational data as objects, providing an easy way to integrate data validation and business logic rules into your application.

Getting started with the solution

The first step is to open Visual Studio .NET 2008, and create a Console Project (see Figure 1).

Figure 1

Then, go to the installation of SQL Server Compact Edition in $PROGRAMFILES$\Microsoft SQL Compact Edition\v3.5\Samples and copy the Northwind.sdf file into the solution directory. Then, open a Command console and change to the solution directory.

Let's call the SQLMetal.exe command in the Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ directory in order to generate the code and mapping for the LINQ to SQL component of the .NET framework (see Figure 2).

Figure 2

Now, let's add the Northwind.cs file to the solution and a reference to the System.Data.Linq.dll assembly (see Figure 3).

Figure 3

And finally, the sample code is shown in Listing 1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 

namespace LINQ_SQLCompact
{
    class Program
    {
        static void Main(string[] args)
        {
            string strConnString = "Northwind.sdf";
            Northwind dbNorthwind = new Northwind(strConnString);
 
            var query = from c in dbNorthwind.Customers
                        where c.City == "Paris"
                        select c;
 
            foreach (Customers c in query)
            {
                System.Console.WriteLine("ContactName={0}, Address={1}, City={2}", 
                                         c.ContactName, c.Address, c.City);
            }
 
            System.Console.WriteLine("Press any key to finish ...");
            System.Console.Read(); 
       } 
    } 
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

merlin981
Software Developer (Senior)
United States United States
Winner - Best Mobile App - AT&T Developer Summit, Las Vegas, 2013
 
My personal resume can be found at: http://www.philippiercedeveloper.com
 
My game portfolio can be found at: http://www.rocketgamesmobile.com
 
About Philip Pierce:
 
I am a software developer with twenty years experience in game development, mobile, web, desktop, server, and database. My extensive background highlights an expertise in rapid application development using the latest Microsoft, Mobile, and Game Development technologies, along with the ability to create AI for games and business software, redesign existing software, develop multi-threaded software, and create client/server applications.

Comments and Discussions

 
GeneralProblem with Name space PinmemberDBNelson20-May-09 13:34 
GeneralMy vote of 2 PinmemberJosh Fischer13-May-09 1:55 
GeneralRe: My vote of 2 PinmemberRichard MacCutchan19-May-09 9:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 12 May 2009
Article Copyright 2009 by merlin981
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid