Skip to content

DataAccess.ScalarSource

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

Home / DataAccess

If a method returns a scalar value, this attribute can be used to specify how database returns this value. The ScalarSource attribute take a parameter of the ScalarSourceType type:

ScalarSourceType Description
DataReader Calls the DbManager.ExecuteReader method, and then calls IDataReader.GetValue method to read the value.
OutputParameter Calls the DbManager.ExecuteNonQuery method, and then reads value from the IDbDataParameter.Value property.
ReturnValue Calls the DbManager.ExecuteNonQuery method, and then reads return value from command parameter collection.
AffectedRows Calls the DbManager.ExecuteNonQuery method, and then returns its return value.

ScalarSource.cs

using System;

using NUnit.Framework;

using BLToolkit.Data;
using BLToolkit.DataAccess;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class ScalarSource
    {
        public abstract class TestAccessor : DataAccessor
        {
            [ScalarSource(ScalarSourceType.DataReader)]
            public abstract int Scalar_DataReader();

            [ActionName("Scalar_DataReader")]
            [ScalarSource(ScalarSourceType.DataReader, 1)]
            public abstract string Scalar_DataReader2();

            [ActionName("Scalar_DataReader")]
            [ScalarSource(ScalarSourceType.DataReader, "stringField")]
            public abstract string Scalar_DataReader3();

            [ScalarSource(ScalarSourceType.OutputParameter)]
            public abstract int Scalar_OutputParameter();

            [ActionName("Scalar_OutputParameter")]
            [ScalarSource(ScalarSourceType.OutputParameter, 1)]
            public abstract string Scalar_OutputParameter2();

            [ActionName("Scalar_OutputParameter")]
            [ScalarSource(ScalarSourceType.OutputParameter, "outputString")]
            public abstract string Scalar_OutputParameter3();

            [ScalarSource(ScalarSourceType.ReturnValue)]
            public abstract int Scalar_ReturnParameter();

            [ActionName("Scalar_DataReader")]
            [ScalarSource(ScalarSourceType.AffectedRows)]
            public abstract int Scalar_AffectedRows();

            public static TestAccessor CreateInstance()
            {
                return (TestAccessor)CreateInstance(typeof(TestAccessor));
            }
        }

        [Test]
        public void DataReaderTest()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            int expectedValue = 12345;
            int actualValue   = ta.Scalar_DataReader();

            Assert.AreEqual(expectedValue, actualValue);
        }

        [Test]
        public void DataReader2Test()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            string expectedValue = "54321";
            string actualValue   = ta.Scalar_DataReader2();

            Assert.AreEqual(expectedValue, actualValue);
        }

        [Test]
        public void DataReader3Test()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            string expectedValue = "54321";
            string actualValue   = ta.Scalar_DataReader3();

            Assert.AreEqual(expectedValue, actualValue);
        }

        [Test]
        public void OutputParameterTest()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            int expectedValue = 12345;
            int actualValue   = ta.Scalar_OutputParameter();

            Assert.AreEqual(expectedValue, actualValue);
        }

        [Test]
        public void OutputParameter2Test()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            string expectedValue = "54321";
            string actualValue   = ta.Scalar_OutputParameter2();

            Assert.AreEqual(expectedValue, actualValue);
        }

        [Test]
        public void OutputParameter3Test()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            string expectedValue = "54321";
            string actualValue   = ta.Scalar_OutputParameter3();

            Assert.AreEqual(expectedValue, actualValue);
        }

        [Test]
        public void ReturnParameterTest()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            int expectedValue = 12345;
            int actualValue   = ta.Scalar_ReturnParameter();

            Assert.AreEqual(expectedValue, actualValue);
        }

        [Test]
        public void AffectedRowsTest()
        {
            TestAccessor ta = TestAccessor.CreateInstance();

            int expectedValue = -1;
            int actualValue   = ta.Scalar_AffectedRows();

            Assert.AreEqual(expectedValue, actualValue);
        }
    }
}

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