Click here to Skip to main content
13,086,264 members (57,584 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 21 Sep 2007

DLinq Quick Start

, 4 Oct 2007
Rate this:
Please Sign up or sign in to vote.
Start using DLinq in easy steps


I am going to show the steps to start using DLinq in a project. Visual Studio 2008 will have LINQ support built in. To use it with Visual Studio 2005, we need to get LINQ installer downloaded and installed.

Creating a New Project

After we install the Linq setup, a new project type is added to Visual Studio 2005 project types tree. We now add a LINQ Console Application from File > New Project menu.

Screenshot - linq_prj.gif

Now we create a table in the database.

Name NVARCHAR(50),
Password NVARCHAR(30),
EmailAddress NVARCHAR(50),
LastLogon DateTime

And now add our mapping object class User:

public class User
    private string logonID;
    private string name;
    private string password;
    private string emailAddress;
    private DateTime lastLogon;
    public User()
    [Column (Id=true, Storage = "logonID")]
    public string LogonID
        get { return logonID; }
        set { logonID = value; }
    [Column(Storage = "name")]
    public string Name
        get { return name; }
        set { name = value; }
    [Column(Storage = "password")]
    public string Password
        get { return password; }
        set { password = value; }
    [Column(Storage = "emailAddress")]
    public string EmailAddress
        get { return emailAddress; }
        set { emailAddress = value; }
    [Column(Storage = "lastLogon")]
    public DateTime LastLogon
        get { return lastLogon; }
        set { lastLogon = value; }

We assign table name with each class with Table attribute. Here we use [Table(Name="users")] to assign the User class to "users" table. We then add Column attribute with parameters. We can assign Storage parameter to the actual class private field. We have boolean Id parameter to assign a property as ID field.

Loading Data

Now we need to create a DataContext object.

DataContext db = new DataContext(
    @"Data Source=localhost\sqlexpress;Initial Catalog=master;Integrated Security=True"

We provide the database connection string as the constructor parameter. Now we create a table and load all users from database.

Table<User> userTable = db.GetTable<User>();
db.Log = Console.Out;
var users = from c in userTable 
    select c;
foreach (User u in users) { 

All right, we have made it work. Now let's add some functionalities.

Insert Operation

To insert, we create a new instance of our mapping object and add it to the table:

User u1= new User();
u1.Name="Maruf Maniruzzaman";

We call SubmitChanges to apply the changes.

Modify Operation

First, we load a single user.

User u2 = userTable.Single(u => u.LogonID == "manir"); 

This is equivalent to:

select * from users u where u.LogonID="manir" limit 1

It returns a single row. We now change some property of the object and update the row in database.


Yes, u2 is a live object and if we change any property of it and submit, the database row is changed accordingly.

Delete Operation

Here we load the object from table and call Remove method of Table to delete.

User u3 = userTable.Single(u => u.LogonID == "maruf");

One thing should be noted that we can do multiple operations and call the SubmitChanges to DataContext object once to apply all changes. So, next comes transaction management.

Database Transaction

We want the previous operations in a single atomic step. We need to manage the transaction. DLINQ makes it very simple. Just:

db.LocalTransaction = db.Connection.BeginTransaction();
    User u1= new User();
    u1.Name="Maruf Maniruzzaman";

    User u2 = userTable.Single(u => u.LogonID == "sumi"); 
    User u3 = userTable.Single(u => u.LogonID == "maruf");
    //We now submit all changes
    //Last statement is commit      
}catch(Exception ex)
    // On any error we rollback everything
//We are done with the LocalTransaction at this point
db.LocalTransaction = null;

It is also possible (and a better way) to wrap the db.SubmitChanges() in a transaction scope this way:

using(TransactionScope ts = new TransactionScope()) {
        //Do some operations.....

Using the Designer

We have, until now, added the mapping objects manually. Now we use the DLinq designer to do that for us. We can drag tables from Server/Database Explorer to create mapping objects, create association (relation) between them using tools graphically.

First we add a new DLinqObjects file to the project.

Project -> Add New Item -> DLinqObjects

Screenshot - add-designer.gif

A drawing surface will be created. We do our drawing here.

Now we drag the "users" table from the Server Explorer's connection tree and drop it on the drawing surface. The table is shown in graphical format. Add all tables you may require for the project.

Screenshot - design-table.gif

Now build the project and mapping classes for the corresponding tables are generated in backing C# file.

You use the generated classes as we used manually created classes. You can also create a Object Datasource from the generated classes. To do that from Data menu, select Add New Data Source and then select Object type and next select the class (for example, User class now) class from the classes tree. You can use it with data controls.

SQL Metal - The Entity Class Generator Tool

With the distribution of LINQ, we get an entity class generator. We can use it to generate entity classes directly or generate an XML file that describes the metadata and then from that XML file, we can generate the entity classes. In command line, we use the following syntax:

>sqlmetal [options] [<input file>]

For example:

>>sqlmetal /server:localhost\SQLEXPRESS /database:master 
    /namespace:MyPrj /code:users.cs

Use this tool to generate entity classes and save duplicate work.

Automatically Exporting Schema

We may want that DLINQ creates the database schema automatically for us. One way to do that is we need to subclass the DataContext class and define all tables in it as public members. So, we define OurDataContext class as following:
public class OurDataContext: DataContext 
    public Table<Users> users; 
    public OurDataContext(string connection) : base(connection) {} 

//Now call following to export schema...
DataContext db = new OurDataContext(
   @"Data Source=localhost\sqlexpress;Initial Catalog=master;Integrated Security=True"

That's it. It will create the users table from the metadata.

OK, maybe, it can help just as a quick start. I'll provide more information as I continue. I will improve this article (along with other 3 articles) when I get time. I have made a lot of modification after I have posted the article. I'll show you one real life example - how to design Data Access Layer of a business application. But I cannot do that in a single step. I'll write as I learn to map the knowledge of same of J2EE with .NET. Thank you.


  • LINQ Hands On Labs
    Also the LINQ installation folder has a lot of documents. This article is a summary of those.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Maruf Maniruzzaman
Software Developer Microsoft
United States United States
Have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).

Story books (specially Masud Rana series), tourism, songs and programming is most favorite.

Maruf Notes

You may also be interested in...


Comments and Discussions

Generaldlinq Pin
nisha885-Sep-10 21:19
membernisha885-Sep-10 21:19 
GeneralLike duplicated work Pin
Michael Sync23-Sep-07 18:14
memberMichael Sync23-Sep-07 18:14 
GeneralRe: Like duplicated work Pin
Maruf Maniruzzaman23-Sep-07 19:28
memberMaruf Maniruzzaman23-Sep-07 19:28 
GeneralRe: Like duplicated work Pin
Michael Sync23-Sep-07 20:37
memberMichael Sync23-Sep-07 20:37 
GeneralArgh! Pin
Marc Clifton22-Sep-07 13:41
protectorMarc Clifton22-Sep-07 13:41 
GeneralRe: Argh! [modified] Pin
Maruf Maniruzzaman22-Sep-07 16:59
memberMaruf Maniruzzaman22-Sep-07 16:59 
GeneralRe: Argh! Pin
Marc Greiner3-Oct-07 2:11
memberMarc Greiner3-Oct-07 2:11 
GeneralRe: Argh! [modified] Pin
Maruf Maniruzzaman3-Oct-07 19:45
memberMaruf Maniruzzaman3-Oct-07 19:45 
GeneralRe: Argh! Pin
Maruf Maniruzzaman6-Oct-07 16:09
memberMaruf Maniruzzaman6-Oct-07 16:09 
GeneralRe: Argh! Pin
norm .net23-Apr-08 22:57
membernorm .net23-Apr-08 22:57 
GeneralRe: Argh! Pin
Marc Clifton24-Apr-08 2:23
protectorMarc Clifton24-Apr-08 2:23 
GeneralRe: Argh! Pin
norm .net24-Apr-08 2:55
membernorm .net24-Apr-08 2:55 
GeneralNot much flesh on these bones Pin
Rob Graham22-Sep-07 3:45
memberRob Graham22-Sep-07 3:45 
GeneralRe: Not much flesh on these bones Pin
Maruf Maniruzzaman22-Sep-07 6:00
memberMaruf Maniruzzaman22-Sep-07 6:00 
GeneralRe: Not much flesh on these bones Pin
norm .net22-Sep-07 8:10
membernorm .net22-Sep-07 8:10 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 4 Oct 2007
Article Copyright 2007 by Maruf Maniruzzaman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid