ExecuteList

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
  ExecuteList.cs
using System;
using System.Collections;
using System.Collections.Generic;

using NUnit.Framework;

using BLToolkit.DataAccess;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class ExecuteList
    {
        public abstract class PersonAccessor : DataAccessor
        {
            // This method reads a list of Person objects.
            //
            [ActionName("SelectAll")]
            public abstract List<Person> GetPersonList1();

            // Here we help the method to get object type information.
            // ObjectTypeAttribute can be applied to the class itself.
            // In this case there is no need to specify object type for each method.
            // Another way to specify object type is a generic parameter
            // of the DataAccessor<T> class.
            //
            [SqlQuery("SELECT * FROM Person")]
            [ObjectType(typeof(Person))]
            public abstract ArrayList GetPersonList2();

            // This method reads a list of scalar values.
            //
            [SqlQuery("SELECT PersonID FROM Person")]
            public abstract List<int> GetPersonIDList();
        }

        [Test]
        public void Test()
        {
            PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();

            // ExecuteList.
            //
            IList list;
            
            list = pa.GetPersonList1();
            list = pa.GetPersonList2();

            foreach (Person p in list)
                Console.WriteLine("{0}: {1} {2}", p.ID, p.FirstName, p.LastName);

            // ExecuteScalarList.
            //
            List<int> slist = pa.GetPersonIDList();

            foreach (int id in slist)
                Console.WriteLine("{0}", id);
        }
    }
}
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]