Сообщений 0 Оценка 27 Оценить |
Для чтения списка бизнес объектов из базы данных используется один из методов BLToolkit.Data.DbManager.ExecuteList:
using System; using System.Collections.Generic; using NUnit.Framework; using BLToolkit.Data; using BLToolkit.Mapping; using BLToolkit.Reflection; namespace HowTo.Data { [TestFixture] public class ExecuteObjectList { [MapValue(Gender.Female, "F")] [MapValue(Gender.Male, "M")] [MapValue(Gender.Unknown, "U")] [MapValue(Gender.Other, "O")] public enum Gender { Female, Male, Unknown, Other } [MapField("PersonID", "ID")] public class Person { public int ID; public string LastName; public string FirstName; public string MiddleName; public Gender Gender; } IList<Person> GetPersonListSqlText() { using (DbManager db = new DbManager()) { return db .SetCommand("SELECT * FROM Person") .ExecuteList<Person>(); } } IList<Person> GetPersonListSproc() { using (DbManager db = new DbManager()) { return db .SetSpCommand("Person_SelectAll") .ExecuteList<Person>(); } } [Test] public void SqlText() { IList<Person> list = GetPersonListSqlText(); foreach (Person p in list) TypeAccessor.WriteDebug(p); } [Test] public void Sproc() { IList<Person> list = GetPersonListSproc(); foreach (Person p in list) TypeAccessor.WriteDebug(p); } } } |
SQL скрипт для создания базы данных:
--CREATE DATABASE BLToolkitData ON PRIMARY --( NAME = N'BLToolkitTest', FILENAME = N'C:\Data\MSSQL.1\MSSQL\DATA\BLToolkitData.mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB ) --LOG ON --( NAME = N'BLToolkitTest_log', FILENAME = N'C:\Data\MSSQL.1\MSSQL\DATA\BLToolkitData_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10% ) --GO -- Person Table IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('Person') AND type in (N'U')) BEGIN DROP TABLE Person END CREATE TABLE Person ( PersonID int NOT NULL IDENTITY(1,1) CONSTRAINT PK_Person PRIMARY KEY CLUSTERED, FirstName nvarchar(50) NOT NULL, LastName nvarchar(50) NOT NULL, MiddleName nvarchar(50) NULL, Gender char(1) NOT NULL CONSTRAINT CK_Person_Gender CHECK (Gender in ('M', 'F', 'U', 'O')) ) ON [PRIMARY] GO INSERT INTO Person (FirstName, LastName, Gender) VALUES ('John', 'Pupkin', 'M') GO -- Person_SelectAll IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Person_SelectAll') BEGIN DROP Procedure Person_SelectAll END GO CREATE Procedure Person_SelectAll AS SELECT * FROM Person GO GRANT EXEC ON Person_SelectAll TO PUBLIC GO |
App.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key = "ConnectionString" value = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"/> </appSettings> </configuration> |
Сообщений 0 Оценка 27 Оценить |