Archive for April 2010

LINQPad with Databases

LINQPad is a fantastic free utility from author and C# guru Joesph Albahari. Per the tag line on the LINQPad site “Kiss goodbye to SQL Management Studio!” and I would suggest that if you are a SQL Server Management Studio user and also interested in LINQ that you take the LINQPad Replacement Challenge!

I didn’t even know about LINQPad until a few years ago when I got the (then 3.0) C# 4.0 in a Nutshell book. It is one of those companion applications that plays a role in the book text and one of the important hooks between the book and the application is that when you registered your copy of the book with the site you received a code that enabled Autocompletion. Not sure if that is still an option or not.

So what the heck is it already? Well LINQPad is many things. It is an application that will allow you to run LINQ queries against a number of things, Objects, LINQ to SQL, Entity Framework, XML, WCF Data Services (OData), SQLite and MySQL as well as a few others. It has the Snippet Compiler feel where you can write a couple of lines of code and F5 it to see what it does. All around a good LINQ-to-* learning tool.

So show me the code already.

First we need a data store to hook up to LINQPad. Let’s have a little taste of SQLite for some sample data. I’ve download a local copy of SQLite and stubbed up a simple database called MyDatabase.db. SQLite has a command line syntax that you can look into. For the point of this post I started SQLite and used the .Read function and supplied a external file name with the contents of the file below.

BEGIN TRANSACTION;
CREATE TABLE People (Id INTEGER PRIMARY KEY, FirstName varchar(25), LastName varchar(50), JokerRating int);
INSERT INTO People VALUES(1,'Chuck','Norris',0);
INSERT INTO People VALUES(2,'Clint','Eastwood',0);
INSERT INTO People VALUES(3,'Pauly','Shore',1);
INSERT INTO People VALUES(4,'Eric','Williams',2);
COMMIT;

Now start up LINQPad and click on the standard Add Connection function. Here you’ll see a few nonstandard options, at least from the typical items you’d expect when you ‘add a connection’ from most other windows developer tools.

LINQPad_ChooseDataContext

If you click on the ‘View more drivers…’ button you’ll find options for other third party LINQPad drivers. In this case we’re looking for the SQlite drivers.

LINQPad_Choose_a_driver

The driver install for the SQLite driver was pretty slick and seem less and you’ll end up with a third item in ‘Build data context automatically’ section. Select the IQ driver and the SQLite provider and the location of the SQLite database created earlier.

LINQPad_ChooseDataContext_SQLLite_Driver

Now you have an available connection to execute queries against. For the Visual Studio inclined this is the Server Explorer idea.

LINQPad_Current_Connections

Now for a couple of simple queries. The first one if probably the simplest query you could do. Selecting everything. Notice the handy output display it gives you.

LINQPad_Simple_LINQ_QueryAll

And this one has a simple filter to show how you can limit the data you want to return. Nothing but the serious folks listed here and also notice the SQL tab is selected to show you the SQL that is being executed against the given database.

LINQPad_Simple_LINQ_Query_With_Where_Clause

Hopefully this will give you enough of a taste to want to try it out for yourself.