Click or drag to resize

IDatabaseExecuteScalar Method

Execute the SQL query and returns the value of first column of the first row in the result set, or null if the result is empty.

Namespace:  AskiaFieldCom.SqlDatabase
Assembly:  AskiaFieldCom (in AskiaFieldCom.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
Object ExecuteScalar(
	string sql,
	IDictionary<string, Object> parameters
)

Parameters

sql
Type: SystemString
SQL query to execute.
parameters
Type: System.Collections.GenericIDictionaryString, Object
List of SQL parameters to apply on the query.

Return Value

Type: Object
Returns the value of first column of the first row in the result set, or null if the result is empty
Remarks
This is useful for insert query, if you would like to obtain the identifier of the new row created.
Remarks
Examples

Insert a new record and get his identifier.

var newRecordId = (int)db.ExecuteScalar("INSERT INTO [MyTable]([Field1], [Field2]) VALUES (@field1, @field2); SELECT SCOPE_IDENTITY();", new Dictionary<string, object> {
    {"@field1", "abc"},
    {"@field2", 123}
});
See Also