Skip to content

DataAccess.ExecuteList

Igor Tkachev edited this page May 21, 2016 · 1 revision

Home / DataAccess

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>
    <connectionStrings>
        <add
            name             = "DemoConnection"
            connectionString = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"
            providerName     = "System.Data.SqlClient" />
    </connectionStrings>
</configuration>

CreateSql

Clone this wiki locally