SqlQuery

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

The SqlQuery attribute allows specifying SQL statement.

SqlQuery.cs
using System;
using System.Collections.Generic;

using NUnit.Framework;

using BLToolkit.DataAccess;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class SqlQuery
    {
        public abstract class TestAccessor : DataAccessor
        {
            [SqlQuery(@"
                SELECT
                    *
                FROM
                    Person
                WHERE
                    FirstName like @firstName AND
                    LastName  like @lastName")]
            public abstract List<Person> GetPersonListByName(string @firstName, string @lastName);
        }

        [Test]
        public void Test()
        {
            TestAccessor ta = DataAccessor.CreateInstance<TestAccessor>();

            List<Person> list = ta.GetPersonListByName("John", "P%");

            Assert.AreNotEqual(0, list.Count);
        }
    }
}
Person.cs
using System;

using BLToolkit.DataAccess;
using BLToolkit.Mapping;

namespace HowTo.DataAccess
{
    public class Person
    {
        [MapField("PersonID"), PrimaryKey, NonUpdatable]
        public int    ID;

        public string LastName;
        public string FirstName;
        public string MiddleName;
        public Gender Gender;
    }
}
Gender.cs
using System;

using BLToolkit.Mapping;

namespace HowTo.DataAccess
{
    public enum Gender
    {
        [MapValue("F")] Female,
        [MapValue("M")] Male,
        [MapValue("U")] Unknown,
        [MapValue("O")] Other
    }
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add
            key   = "ConnectionString"
            value = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
    </appSettings>
</configuration>
Create.sql script
 
© 2010 www.bltoolkit.net
[email protected]