1 Previous Next 

Using Linq to Sql with Sql Compact Edition

The Linq to Sql supports Sql compact edition unfortunately the designer for Visual Studio 2008 does not.  To generate the dbml file use the SqlMetal.exe utility.


First add the sql compact edition database to your project and cancel out of the wizard to generate a typed dataset.  For this example I added the Northwind database.  Open the Visual Studio 2008 command prompt go to the projects directory.  The command to generate the dbml is Sqlmetal.exe /dbml:northwind.dbml northwind.sdf /language:vb


After the file is generated from the project menu select add existing item and add the dbml file just generated. 


Sample Linq query


Dim db As New Northwind("Data Source= Northwind.sdf")
db.Log = Console.Out
Dim q = From p In db.Products _
        Where p.UnitsInStock > 3 _
        Select p.ProductName, p.UnitPrice

Dim bs As New BindingSource

bs.DataSource = q
DataGridView1.DataSource = bs

1 Previous Next