Format

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

The Format indicates that the specified parameter is used to construct the stored procedure name or SQL statement:

Format.cs
using System;
using System.Collections.Generic;

using NUnit.Framework;

using BLToolkit.DataAccess;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class Format
    {
        public abstract class PersonAccessor : DataAccessor
        {
            [SqlQuery("SELECT TOP {0} * FROM Person")]
            public abstract List<Person> GetPersonList([Format] int top);
        }

        [Test]
        public void Test()
        {
            PersonAccessor pa   = DataAccessor.CreateInstance<PersonAccessor>();
            List<Person>   list = pa.GetPersonList(2);

            Assert.That(list,       Is.Not.Null);
            Assert.That(list.Count, Is.LessThanOrEqualTo(2));
        }
    }
}
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]