-
Notifications
You must be signed in to change notification settings - Fork 113
Data.Parameters
Igor Tkachev edited this page May 20, 2016
·
1 revision
The DbManager.Parameter method allows assigning parameters to an SQL query, setting parameter values before query, and getting return/out values after.
Parameter.cs
using System;
using System.Data;
using NUnit.Framework;
using BLToolkit.Data;
namespace HowTo.Data
{
using DataAccess;
[TestFixture]
public class Parameter
{
[Test]
public void AssignParameterTest()
{
using (DbManager db = new DbManager())
{
int n = db
.SetCommand("SELECT @par1 + @par2",
db.Parameter("@par1", 2),
db.Parameter("@par2", 2))
.ExecuteScalar<int>();
Assert.AreEqual(4, n);
}
}
[Test]
public void SetValueTest()
{
using (DbManager db = new DbManager())
{
db.SetCommand("SELECT @par * 2",
db.Parameter("@par", DbType.Int32));
db.Parameter("@par").Value = 2;
Assert.AreEqual(4, db.ExecuteScalar<int>());
}
}
[Test]
public void ReturnValueTest()
{
using (DbManager db = new DbManager())
{
/*
* CREATE Function Scalar_ReturnParameter()
* RETURNS int
* AS
* BEGIN
* RETURN 12345
* END
*/
db
.SetSpCommand("Scalar_ReturnParameter")
.ExecuteNonQuery();
int n = (int)db.Parameter("@RETURN_VALUE").Value;
Assert.AreEqual(12345, n);
}
}
[Test]
public void ReturnValueTest2()
{
using (DbManager db = new DbManager())
{
int n = db
.SetSpCommand("Scalar_ReturnParameter")
.ExecuteScalar<int>(ScalarSourceType.ReturnValue);
Assert.AreEqual(12345, n);
}
}
[Test]
public void OutputParameterTest()
{
using (DbManager db = new DbManager())
{
/*
* CREATE Procedure Scalar_OutputParameter
* @outputInt int = 0 output,
* @outputString varchar(50) = '' output
* AS
* BEGIN
* SET @outputInt = 12345
* SET @outputString = '54321'
* END
*/
db
.SetSpCommand("Scalar_OutputParameter",
db.OutputParameter("@outputInt", 1),
db.OutputParameter("@outputString", "1"))
.ExecuteNonQuery();
Assert.AreEqual(12345, (int) db.Parameter("@outputInt"). Value);
Assert.AreEqual("54321", (string)db.Parameter("@outputString").Value);
}
}
[Test]
public void OutputParameterAsReturnValueTest()
{
using (DbManager db = new DbManager())
{
string returnValue = db
.SetSpCommand("Scalar_OutputParameter")
.ExecuteScalar<string>(ScalarSourceType.OutputParameter, "outputString");
Assert.AreEqual("54321", returnValue);
}
}
[Test]
public void CreateParametersTest()
{
Person person = new Person();
person.FirstName = "John";
person.LastName = "Smith";
person.Gender = Gender.Male;
using (DbManager db = new DbManager())
{
db.BeginTransaction();
// Prepare command.
//
int id = db
.SetSpCommand("Person_Insert",
db.CreateParameters(person))
.ExecuteScalar<int>();
// Check the result.
//
person = db
.SetCommand(
"SELECT * FROM Person WHERE PersonID = @id",
db.Parameter("@id", id))
.ExecuteObject<Person>();
Assert.IsNotNull(person);
// Cleanup.
//
db
.SetCommand(
"DELETE FROM Person WHERE PersonID = @id",
db.Parameter("@id", id))
.ExecuteNonQuery();
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>