Very short and simple example of using SqlMetal

Very short and simple example of using SqlMetal

  • Comments 2

A simple sample of how to use SqlMetal.


"Code Generation Tool (SqlMetal.exe)"


This tool will create mappings and code for LINQ to SQL.

So, as mentioned, simple and short. Create a database, some tables and insert some data.


create database VeryMetal


use VeryMetal


create table Users (UserId int primary key, FirstName nvarchar(10), LastName nvarchar(10), DepartmentId int)


create table Departments(DepartmentId int primary key, DepartmentName nvarchar(10))



--Insert some users and departments

insert into Users values (1, 'John', 'Johnson', 1)

insert into Users values (2, 'Paul', 'Paulson', 2)

insert into Users values (3, 'Mike', 'Mikeson', 1)

insert into Users values (4, 'Mary', 'Maryson', 2)


insert into Departments values (1, 'Support')

insert into Departments values (2, 'Finance')


Then fire up the Visual Studio Command Prompt and run the following:


Sqlmetal /server:<your server> /database:VeryMetal /code:C:\Temp\MetalCode.cs /map:C:\Temp\MetalMap.xml /namespace:VeryMetalEntities


This should now have generated the .cs and .xml files.


So, create new console application, add the MetalCode file to the project and enter the following code:


        static void Main(string[] args)




                String cs = @"Data Source=<your server>;Initial Catalog=VeryMetal;Integrated Security=True";

                String file = @"C:\Temp\MetalMap.xml";


                var dc = new VeryMetal(cs, XmlMappingSource.FromUrl(file));

                var users = from u in dc.Users where u.DepartmentId == 1 select u;


                Console.WriteLine("All users in Support Department.");

                foreach (var user in users)


                    Console.WriteLine("{0} - {1}", user.FirstName, user.LastName);



                Console.WriteLine("\nGet single user with firstname starting with Pau ");


                Table<Users> ut = dc.GetTable<Users>();

                var users2 = ut.Single(x => x.FirstName.StartsWith("Pau"));

                Console.WriteLine("{0} - {1}", users2.FirstName, users2.LastName);



            catch (Exception ex)






Output should be:


All users in Support Department.

John - Johnson

Mike - Mikeson


Get single user with firstname starting with Pau

Paul - Paulson

Leave a Comment
  • Please add 5 and 8 and type the answer here:
  • Post
  • Dude - use variable names which HELP in describing what you are doing, especially since this is an "example".  "String cs" and String file" are truly useless variable names.  File?  What file?  Oh a "Mapping File".  And a "dc", yeah, like Washington DC I guess.  It is supposed to be an 'e'.  What?  Oh yeah, the 'e' stands for 'example' - sorry.

    Lastly, put in a foreign key and/or at least do your parent/child inserts in the right order. Parent always comes first, then the child.

    These are simple, obvious, common sense things that every developer should follow - but few actually do.

  • Wow Bill, try kicking your dog first before commenting.

Page 1 of 1 (2 items)