Skip to content
Igor Tkachev edited this page May 20, 2016 · 1 revision

Home / Data

Typical scenario of using DbManager includes the following steps:

•Create a DbManager class instance. •Set an SQL command or a stored procedure name. •Call an ExecuteXXX method.

The SetSpCommand method is used to set a stored procedure name and to provide command parameters, if any.

SetSpCommand.cs

using System;
using System.Collections.Generic;

using NUnit.Framework;

using BLToolkit.Data;
using BLToolkit.Mapping;

namespace HowTo.Data
{
    using DataAccess;

    [TestFixture]
    public class SetSpCommand
    {
        // Select a person list.
        //
        public IList<Person> GetPersonList()
        {
            using (DbManager db = new DbManager())
            {
                return db
                    .SetSpCommand("Person_SelectAll")
                    .ExecuteList<Person>();
            }
        }

        [Test]
        public void Test1()
        {
            IList<Person> list = GetPersonList();

            Assert.AreNotEqual(0, list.Count);
        }

        // Select a person.
        //
        public Person GetPersonByID1(int id)
        {
            using (DbManager db = new DbManager())
            {
                // Pass a parameter using the Parameter method.
                //
                return db
                    .SetSpCommand("Person_SelectByKey",
                        db.Parameter("@id", id))
                    .ExecuteObject<Person>();
            }
        }

        public Person GetPersonByID2(int id)
        {
            using (DbManager db = new DbManager())
            {
                // Pass a parameter using the params parameter of the SetSpCommand method.
                //
                return db
                    .SetSpCommand("Person_SelectByKey", id)
                    .ExecuteObject<Person>();
            }
        }

        [Test]
        public void Test2()
        {
            Person person = GetPersonByID1(1);
            Assert.IsNotNull(person);

            person = GetPersonByID2(1);
            Assert.IsNotNull(person);
        }

        // Insert, Update, and Delete a person.
        //
        public Person GetPersonByID(DbManager db, int id)
        {
            return db
                .SetSpCommand("Person_SelectByKey", id)
                .ExecuteObject<Person>();
        }

        public Person CreatePerson(DbManager db)
        {
            int id = db
                .SetSpCommand("Person_Insert",
                    db.Parameter("@LastName",   "Frog"),
                    db.Parameter("@MiddleName", null),
                    db.Parameter("@FirstName",  "Crazy"),
                    db.Parameter("@Gender",     Map.EnumToValue(Gender.Male)))
                .ExecuteScalar<int>();

            return GetPersonByID(db, id);
        }

        public Person UpdatePerson(DbManager db, Person person)
        {
            db
                .SetSpCommand("Person_Update", db.CreateParameters(person))
                .ExecuteNonQuery();

            return GetPersonByID(db, person.ID);
        }

        public Person DeletePerson(DbManager db, Person person)
        {
            db
                .SetSpCommand("Person_Delete", person.ID)
                .ExecuteNonQuery();

            return GetPersonByID(db, person.ID);
        }

        [Test]
        public void Test3()
        {
            using (DbManager db = new DbManager())
            {
                db.BeginTransaction();

                // Insert.
                //
                Person person = CreatePerson(db);

                Assert.IsNotNull(person);

                // Update.
                //
                Assert.AreEqual(Gender.Male, person.Gender);

                person.Gender = Gender.Female;

                person = UpdatePerson(db, person);

                Assert.AreEqual(Gender.Female, person.Gender);

                // Delete.
                //
                person = DeletePerson(db, person);

                Assert.IsNull(person);

                db.CommitTransaction();
            }
        }
    }
}

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>
Clone this wiki locally