Thursday, September 25, 2008

Easy database querying with dOOdads (C#, .NET)

Been some time since I posted about MyGeneration and dOOdads. Still using it regularly, even though it is a few years old now. A few sites I do still use .NET 1.1, so it is still in use.

A brief (re)introduction - MyGeneration is a tool that can generate code for you, through the use of templates (you can create your own, and there is a template library). It comes bundled with the dOOdads data abstraction library, which allows you to update and query your database without knowing the intricacies of interacting with it (i.e. .NET classes to use, SQL to write etc). Both MyGeneration and dOOdads support multiple databases (SQL Server, MySQL, Oracle etc) and regeneration of code, changing the connection string is often all that is needed to move to another system.

It was freeware initially, but is now hosted on SourceForge and no longer actively maintained by the main developers.

A basic example of a query would be:

Employees e = new Employees();
e.Where.EmployeeID.Value = 1;
if(e.Query.Load())
{
    SurnameTextBox.Text = e.Surname;
}

You can even select multiple employees with different EmployeeID's:

Employees e = new Employees();
e.Where.EmployeeID.Value = "1,3,7";
e.Where.EmployeeID.Operator = WhereParameter.Operand.In;
if(e.Query.Load())
{
    ....
}

For columns of type 'bit' (e.g. true, false), where you want to treat NULL as false, you use a TearOff:

Employees e = new Employees();
e.Query.OpenParenthesis();
e.Where.DrivingLicense.Value = false;
WhereParameter driving = e.Where.TearOff.DrivingLicense;
driving.Operator = WhereParameter.Operand.IsNull;
// add conjunction and close parenthesis
driving.Conjuction = WhereParameter.Conj.Or;
e.Query.CloseParenthesis();
if(e.Query.Load())
{
    ....
}

Note: driving.Conjuction is correct, it is a typo in dOOdads.

No comments: