# Saturday, November 08, 2008
« Nemerle Macros - ImplementEquality | Main | New Resource-Oriented Library for ASP.NE... »

Nemerle includes SQL macros. These make writing basic ADO.NET code a lot easier. They won't replace a complex ORM solution, but are fantastic for quick data access code. The main win is compile time query checking! That's right... if you misspell a column, table, etc, you get a compile error.

Watch the Screencast

Before

using System;
using System.Data.SqlClient;
 
namespace Demo
{
    module Program
    {
        Main() : void
        {
            using (conn = SqlConnection(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=SSPI"))
            {
                conn.Open();
 
                using (cmd = SqlCommand("select Id, FirstName, LastName from Customer where IsPrefered=1", conn))
                {
                    using (reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            def id = reader.GetInt32(0);
                            def firstName = reader.GetString(1);
                            def lastName = reader.GetString(2);
                            Console.WriteLine("{0}: {1} {2}", id, firstName, lastName);
                        }
                    }
                }
            }
 
            _ = Console.ReadLine();
        }
    }
}

After

using System;
using System.Data.SqlClient;
using Nemerle.Data;
 
[assembly: ConfigureConnection("System.Data.SqlClient.SqlConnection", @"Data Source=(local)\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=SSPI")]
 
namespace Demo
{
    module Program
    {
        Main() : void
        {
            using (conn = SqlConnection(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=SSPI"))
            {
                conn.Open();
 
                ExecuteReaderLoop("select id, firstName, lastName from Customer where IsPrefered=1", conn, {
                    Console.WriteLine("{0}: {1} {2}", id, firstName, lastName);
                });
            }
 
            _ = Console.ReadLine();
        }
    }
}
Saturday, November 08, 2008 5:59:28 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
Sunday, November 09, 2008 11:45:48 PM (GMT Standard Time, UTC+00:00)
Does Nemerle have built in deep copying of objects?
If not that would be a good candidate for another handy macro.
Have you got these macros into the Nemerle base revision? Great work!
Comments are closed.