In this example we going to use the data from the local SQL server selecting the meta data data from the sysobjects table and executing the results into single return type.
public class ExampleReadDataSingleValue: SqlServerDataAccess
{
public ExampleReadDataSingleValue() : base(
new DataAccessConfig("SampleConfig", new DataAccessConfigOptions { ConnectionStringKey = "notused" },
new HardCodedConnectionStringsResolver("Server=(localdb)\\MSSQLLocalDB;Integrated Security = true"))
)
{
}
public int GetSysObjectsCount()
{
var cmd = CreateTextCommand("Select count(*) from Sysobjects");
return ExecuteScalarAs<int>(cmd);
}
public int? GetValueThatMightBeNull()
{
var cmd = CreateTextCommand("Select id from Sysobjects where 1=2"); // zero rows
return ExecuteScalarAs<int?>(cmd);
}
public int? GetValueThatMightBeNull2()
{
var cmd = CreateTextCommand("Select null as col"); // 1 row value null
return ExecuteScalarAs<int?>(cmd);
}
public int GetValueThatMustBeConverted()
{
var cmd = CreateTextCommand("Select Cast(123 as tinyint) as col");
return ExecuteScalarWithConvertAs<int>(cmd);
}
}
There are four cases when executing the value to a Scalar values:
ExecuteScalarAs<int>
Or is you know you working with DateTime you can use
ExecuteScalarAs<DateTime>
ExecuteScalarWithConvertAs<int>
When doing the conversion you will need to deal with conversion errors , ie tinyint to int will always work, but int to byte will not always work.