MySQL Connector/Net |
MySqlCommand..::.ExecuteScalar Method |
MySqlCommand Class Example See Also Send Feedback |
Executes the query, and returns the first column of the first row in the
result set returned by the query. Extra columns or rows are ignored.
Namespace:
MySql.Data.MySqlClient
Assembly:
MySql.Data (in MySql.Data.dll) Version: 6.2.2.0
Syntax
C# |
---|
public override Object ExecuteScalar() |
Visual Basic (Declaration) |
---|
Public Overrides Function ExecuteScalar As Object |
Visual C++ |
---|
public: virtual Object^ ExecuteScalar() override |
Return Value
The first column of the first row in the result set, or a null reference if the result set is emptyImplements
IDbCommand..::.ExecuteScalar()()()Remarks
Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. This requires less code than using the ExecuteReader()()() method, and then performing the operations necessary to generate the single value using the data returned by a MySqlDataReader
Examples
The following example creates a MySqlCommand and then
executes it using ExecuteScalar. The example is passed a string that is a
SQL statement that returns an aggregate result, and a string to use to
connect to the data source.
CopyVB.NET
Public Sub CreateMySqlCommand(myScalarQuery As String, myConnection As MySqlConnection) Dim myCommand As New MySqlCommand(myScalarQuery, myConnection) myCommand.Connection.Open() myCommand.ExecuteScalar() myConnection.Close() End Sub
CopyC#
public void CreateMySqlCommand(string myScalarQuery, MySqlConnection myConnection) { MySqlCommand myCommand = new MySqlCommand(myScalarQuery, myConnection); myCommand.Connection.Open(); myCommand.ExecuteScalar(); myConnection.Close(); }
CopyC++
public: void CreateMySqlCommand(String* myScalarQuery, MySqlConnection* myConnection) { MySqlCommand* myCommand = new MySqlCommand(myScalarQuery, myConnection); myCommand->Connection->Open(); myCommand->ExecuteScalar(); myConnection->Close(); }