Knight Data Access Layer
Execute Scalar
Basic Tasks C# > Database Commands > Execute Scalar

Use the Execute Scalar to return a single value.  Optional parameters may be passed in.

 

//Get the current instance of the helper

IDataHelper db = DataHelper.SessionFactory();

 

//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> parms = new Dictionary<string, object>();

parms.Add("FirstName", "John");

parms.Add("LastName", "Smith");

string sql = "SELECT COUNT(1) FROM Person WHERE FirstName=@FirstName AND LastName=@LastName";

 

long results = (long)db.ExecuteScalar(sql,parms);