Dynamicweb 8 Documentation
ExecuteScalar(String,String) Method
Example 

The SQL to execute against the database
Name of the MS Access database to get the data from (I.e. "Access.mdb" in /Database directory). Ignored if solution is running MS SQL-Server
Executes the query, and returns the first column of the first row in the resultset returned by the query. Extra columns or rows are ignored.
Syntax
'Declaration
 
Public Overloads Shared Function ExecuteScalar( _ 
   ByVal sql As String, _ 
   ByVal database As String _ 
) As Object
public static object ExecuteScalar( 
   string sql,
   string database 
)

Parameters

sql
The SQL to execute against the database
database
Name of the MS Access database to get the data from (I.e. "Access.mdb" in /Database directory). Ignored if solution is running MS SQL-Server

Return Value

The first column of the first row in the resultset.
Example
Using ExecuteScalarUsing ExecuteScalar
namespace Dynamicweb.Examples.CSharp
{
    class DatabaseExecute
    {

        public void ExecuteSample()
        {
            //ExecuteScalar
            var pageCount = Input.FormatInteger(Database.ExecuteScalar("SELECT COUNT(PageID) FROM Page"));

            //ExecuteScalar on alternate database
            var userCount = Input.FormatInteger(Database.ExecuteScalar("SELECT COUNT(AccessUserID) FROM AccessUser"));

            //ExecuteNonQuery
            var numberOfPagesDeleted = Database.ExecuteNonQuery("DELETE FROM Page WHERE PageID < 101");

            //ExecuteNonQuery on alternate database
            var numberOfUsersDeleted = Database.ExecuteNonQuery("DELETE FROM AccessUser WHERE AccessUserID < 101", "Access.mdb");

        }

    }
}
Public Class DatabaseExecute
    Public Sub ExecuteSample()

        'ExecuteScalar
        Dim pageCount As Integer = Input.FormatInteger(Database.ExecuteScalar("SELECT COUNT(PageID) FROM Page"))

        'ExecuteScalar on alternate database
        Dim userCount As Integer = Input.FormatInteger(Database.ExecuteScalar("SELECT COUNT(AccessUserID) FROM AccessUser"))

        'ExecuteNonQuery
        Dim numberOfPagesDeleted As Integer = Database.ExecuteNonQuery("DELETE FROM Page WHERE PageID < 101")

        'ExecuteNonQuery on alternate database
        Dim numberOfUsersDeleted As Integer = Database.ExecuteNonQuery("DELETE FROM AccessUser WHERE AccessUserID < 101", "Access.mdb")

    End Sub
End Class
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

Database Class
Database Members
Overload List

Send Feedback