Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / SQL

Extending C# to Support SQL Syntax at Compile Time

Rate me:
Please Sign up or sign in to vote.
4.84/5 (29 votes)
23 Jul 2014CPOL13 min read 146.3K   858   76  
Ever wished you could truly embed SQL functionality in your C# code without using strings or late binding? Imagine being able to write complex Where clauses purely in C#.
using System;
using Indy.Data;
using ExampleDB;
using NUnit.Framework;

namespace TestSuite {
	public class DBTest {
		public DBTest() { }

    protected Connection _DB;

    [TestFixtureSetUp] 
		protected void ResetDB() {	
      using (Transaction xTx = new Transaction(_DB)) {
        using (CustomerTbl xCustomers = new CustomerTbl(xTx)) {
          xCustomers.Purge(true);
        }
        using (CountryTbl xCountries = new CountryTbl(xTx)) {
          xCountries.Purge(true);
        }
        xTx.Commit();
      }
      using (Transaction xTx = new Transaction(_DB)) {
        using (CountryTbl xCountries = new CountryTbl(xTx)) {
          CountryTbl.Row xCountry;

          xCountry = new CountryTbl.Row();
          xCountry.CountryID = 1;
          xCountry.CountryName = "Russia";
          xCountry.ISOCode = "RU";
          xCountries.Insert(xCountry);

          xCountry = new CountryTbl.Row();
          xCountry.CountryID = 2;
          xCountry.CountryName = "Cyprus";
          xCountry.ISOCode = "CY";
          xCountries.Insert(xCountry);

          xCountry = new CountryTbl.Row();
          xCountry.CountryID = 3;
          xCountry.CountryName = "United States";
          xCountry.ISOCode = "US";
          xCountries.Insert(xCountry);
        }
        using (CustomerTbl xCustomers = new CustomerTbl(xTx)) {
          CustomerTbl.Row xCustomer;

          xCustomer = new CustomerTbl.Row();
          xCustomer.CustomerID = 1;
          xCustomer.NameFirst = "Chad";
          xCustomer.NameLast = "Hower";
          xCustomer.Tag = 1;
          xCustomer.CountryID = 1;
          xCustomer.Log = "Russia, Cyprus, Jordan, Turkey";
          xCustomers.Insert(xCustomer);

          xCustomer = new CustomerTbl.Row();
          xCustomer.CustomerID = xCustomers.NewKey();
          xCustomer.NameFirst = "Hadi";
          xCustomer.NameLast = "Hariri";
          xCustomer.Tag = 2;
          xCustomer.CountryID = 2;
          xCustomers.Insert(xCustomer);

          xCustomer = new CustomerTbl.Row();
          xCustomer.CustomerID = xCustomers.NewKey();
          xCustomer.NameFirst = "Thomas";
          xCustomer.NameLast = "Jefferson";
          xCustomer.CountryID = 3;
          xCustomers.Insert(xCustomer);

          xCustomer = new CustomerTbl.Row();
          xCustomer.CustomerID = xCustomers.NewKey();
          xCustomer.NameFirst = "James";
          xCustomer.NameLast = "Madison";
          xCustomer.CountryID = 3;
          xCustomers.Insert(xCustomer);
        }
        xTx.Commit();
      }
    }

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Cyprus Cyprus
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"

I am a former Microsoft Regional DPE (MEA) covering 85 countries, former Microsoft Regional Director, and 10 Year Microsoft MVP.

I have lived in Bulgaria, Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, The Caribbean, and USA.

Creator of Indy, IntraWeb, COSMOS, X#, CrossTalk, and more.

Comments and Discussions