Search notes:

System.Data.Common.DbCommand (class)

System.Data.Common.DbCommand represents an SQL statement or a stored procedure.
A special case is that the DbCommand text is just the name of table.
The type of the DbCommand can be queried with the CommandType property which returns a System.Data.CommandType enum.
If the DbCommand is used to query data (as is typically the case for a select statement, one of the following two method is needed:
If the DbCommand does not return any data (for example in a DDL statement), the statement needs to be executed with ExecuteNonQuery().
The derived class System.Data.SqlClient.SqlCommand additionally implements XmlReader() which returns an instance of System.Xml.XmlReader.

Parametrizing an DbCommand object

A DbCommand object can be constructed in a «parameterized» way. This allows to execute the same DbCommand multiple times, each time with a different set of parameter-values.
The benefits of parameterized DbCommand objects are:
Parameter-values are passed to a DbCommand using a System.Data.Common.DbParameter object.
The format of parameter-placeholders is dependent on the Data provider:
Data provider Format
System.Data.SqlClient @paramName
System.Data.OleDb ?
System.Data.Odbc ?
System.Data.OracleClient :paramName
MySqlClient.Data.MySqlClient ? is supported, @ is preferred

ExecuteScalar()

ExecuteScalar() provides a convenient way to query a value from a single-row, single-column record set, typically an aggregate function.
The following example counts the record in a table. For simplicity's sake, I have chosen a CSV file for the table and an OLE DB provider to query from it.
using System;
using System.Data.Common;
using System.Data.OleDb;
using System.IO;

class Prg {

   static void Main () {

      string csvFilePath = Directory.GetCurrentDirectory();

      string provider =
         // "Microsoft.Jet.OLEDB.4.0"
            "Microsoft.ACE.OLEDB.12.0";

      string connectionString           =
          $"Provider={provider};"       +
          $"Data Source={csvFilePath};" +
          $"Extended Properties='text';";

      using OleDbConnection connection = new OleDbConnection(connectionString);
      connection.Open();

      DbCommand command = new OleDbCommand("select count(*) from data.csv", connection);
//
//       Of course, the constructor returns an OleDbCommand object. Thus, the
//       following line would probably be more correct. But because this
//       program serves at demonstrating the basic functionality of ExecuteScalar() ,
//       I have not done sol
//
//    OleDbCommand command = new OleDbCommand("select id, num, txt from data.csv", connection);

      Console.WriteLine("The csv file contains {0} records", (Int32) command.ExecuteScalar());

   }
}
Github repository .NET-API, path: /System/Data/Common/DbCommand/ExecuteScalar/countRecords.cs
For the record: the CSV with which I tested the program is:
num,txt
4,four
2,two
9,nine
Github repository .NET-API, path: /System/Data/Common/DbCommand/ExecuteScalar/data.csv

ExecuteReader()

ExecuteReader() returns an instance of System.Data.Common.DbDataReader.
ExecuteReader has an overwritten method that takes an System.Data.CommandBehavior enum that influences the operation of ExecuteReader().

See also

The CreateCommand() method of the System.Data.Common.DbProviderFactory class.

Index