Сообщений 0 Оценка 18 Оценить |
Для чтения бизнес объекта из базы данных используется один из методов BLToolkit.Data.DbManager.ExecuteObject:
using System; using System.Collections.Generic; using NUnit.Framework; using BLToolkit.Data; using BLToolkit.Mapping; using BLToolkit.Reflection; namespace HowTo.Data { [TestFixture] public class ExecuteObject { public enum Gender { [MapValue("F")] Female, [MapValue("M")] Male, [MapValue("U")] Unknown, [MapValue("O")] Other } public abstract class Person { [MapField("PersonID")] public abstract int ID { get; } public abstract string LastName { get; set; } public abstract string FirstName { get; set; } public abstract string MiddleName { get; set; } public abstract Gender Gender { get; set; } } Person GetPersonSqlText(int id) { using (DbManager db = new DbManager()) { return db .SetCommand("SELECT * FROM Person WHERE PersonID = @id", db.Parameter("@id", id)) .ExecuteObject<Person>(); } } Person GetPersonSproc1(int id) { using (DbManager db = new DbManager()) { return db .SetSpCommand("Person_SelectByKey", db.Parameter("@id", id)) .ExecuteObject<Person>(); } } Person GetPersonSproc2(int id) { using (DbManager db = new DbManager()) { return db .SetSpCommand("Person_SelectByKey", id) .ExecuteObject<Person>(); } } [Test] public void SqlText() { Person person = GetPersonSqlText(1); TypeAccessor.WriteConsole(person); } [Test] public void Sproc1() { Person person = GetPersonSproc1(1); TypeAccessor.WriteConsole(person); } [Test] public void Sproc2() { Person person = GetPersonSproc2(1); TypeAccessor.WriteConsole(person); } } } |
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_SelectByKey IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Person_SelectByKey') BEGIN DROP Procedure Person_SelectByKey END GO CREATE Procedure Person_SelectByKey @id int AS SELECT * FROM Person WHERE PersonID = @id GO GRANT EXEC ON Person_SelectByKey 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 Оценка 18 Оценить |