Knight Data Access Layer
ExecuteScalar(String,Dictionary<String,Object>) Method
Example 




KellermanSoftware.NetDataAccessLayer Namespace > IDataHelper Interface > ExecuteScalar Method : ExecuteScalar(String,Dictionary<String,Object>) Method
The scalar SQL
Parameters
Execute the passed query and return the first column of the first row
Syntax
Overloads Function ExecuteScalar( _
   ByVal sql As String, _
   ByVal parameters As Dictionary(Of String,Object) _
) As Object
Dim instance As IDataHelper
Dim sql As String
Dim parameters As Dictionary(Of String,Object)
Dim value As Object
 
value = instance.ExecuteScalar(sql, parameters)
object ExecuteScalar( 
   string sql,
   Dictionary<string,object> parameters
)

Parameters

sql
The scalar SQL
parameters
Parameters

Return Value

First column of first row
Example
//Get the current thread static safe instance of the helper
IDataHelper db = DataHelper.SessionFactory();
 
//Specify User Name and License Key from the receipt, leave blank for trial mode
//db.UserName = "John Smith 1234";
//db.LicenseKey = "asdfl219==";
 
//Log to the console
db.EnableLogging();
 
//Open the database connection
db.OpenConnection();
 
//Create a new Person to save to the database
Person currentPerson = new Person();
currentPerson.FirstName = "John";
currentPerson.LastName = "Smith";
 
//Save it to the database 
db.Save(currentPerson);
 
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("FirstName", "John");
parameters.Add("LastName", "Smith");
 
string sql = "SELECT COUNT(1) FROM Persons WHERE FirstName=@FirstName AND LastName=@LastName";
int results = (int)db.ExecuteScalar(sql,parameters);
 
//Close the connection
db.CloseConnection();
'Get the current thread static safe instance of the helper
Dim db As IDataHelper = DataHelper.SessionFactory()
 
'Specify User Name and License Key from the receipt, leave blank for trial mode
'db.UserName = "John Smith 1234"
'db.LicenseKey = "asdfl219=="
 
'Log to the console
db.EnableLogging()
 
'Open the database connection
db.OpenConnection()
 
'Create a new Person to save to the database
Dim currentPerson As New Person()
currentPerson.FirstName = "John"
currentPerson.LastName = "Smith"
 
'Save it to the database 
db.Save(currentPerson)
 
Dim parameters As New Dictionary(Of String, Object)()
parameters.Add("FirstName", "John")
parameters.Add("LastName", "Smith")
 
Dim sql As String = "SELECT COUNT(1) FROM Persons WHERE FirstName=@FirstName AND LastName=@LastName"
Dim results As Integer = CInt(Fix(db.ExecuteScalar(sql,parameters)))
 
'Close the connection
db.CloseConnection()
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

IDataHelper Interface
IDataHelper Members
Overload List