dir.by  
  Search  
Programming, development, testing
ADO.NET (working with the database on C#)
Get a scalar (single) value: Count, Min, Max, Sum, etc. | ADO.NET, C#
  Looked at 5038 times    
 Get a scalar (single) value: Count, Min, Max, Sum, etc. | ADO.NET, C# 
last updated: 22 July 2025
When writing queries against Database, we can use built-in SQL functions, such as: Min, Max, Sum, Count etc.
These SQL functions return a single value.
To work with such SQL functions, a special method ExecuteScalar is defined in SqlCommand.
Step 1. Creating a new application
Step 2. Let's write the code
  C#     В файле Program.cs напишем код:
using System;

// подключаем SQL ADO.NET
using Microsoft.Data.SqlClient;

namespace ConsoleApplication1
{
     class Program
     {
          static void Main(string[] args)
          {
               // connection string
               string connectionString = @"Data Source=EVGENI; Initial Catalog=MyDatabase1; Integrated Security=True";

               // connect to db
               using (SqlConnection connection = new SqlConnection(connectionString))
               {
                    // open db
                    connection.Open();

                    // init sql Command
                    using (SqlCommand command = new SqlCommand())
                    {
                         // connection
                         command.Connection = connection;
                        
                         // Count
                         command.CommandText = "SELECT COUNT(*) from Books";
                         object result1 = command.ExecuteScalar();
                         // выводим на экран
                         Console.WriteLine("Число записей={0}", result1);

                         // MIN
                         command.CommandText = "SELECT MIN(Price) from Books";
                         object result2 = command.ExecuteScalar();
                         // выводим на экран
                         Console.WriteLine("MIN Цена={0}", result2);

                         // MAX
                         command.CommandText = "SELECT MAX(Price) from Books";
                         object result3 = command.ExecuteScalar();
                         // выводим на экран
                         Console.WriteLine("MAX Цена={0}", result3);

                         // SUM
                         command.CommandText = "SELECT SUM(Price) from Books";
                         object result4 = command.ExecuteScalar();
                         // выводим на экран
                         Console.WriteLine("SUM Цена={0}", result4);
                    }
               }
          }
     }
}
Example result
 
← Previous topic
Read (select) data from the Database using SqlDataAdapter and DataSet | ADO.NET, C#
 
Next topic →
Add data (insert into) to the Database using SqlCommand | ADO.NET, C#
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is ADO.NET?
Let's create a ADO.NET console application (C#, database)
Working with data in ADO.NET
Read the data (select) from the Database using SqlCommand and SqlDataReader in ADO.NET C#. Database Type Mapping
Read (select) data from the Database using SqlDataAdapter and DataSet | ADO.NET, C#
Get a scalar (single) value: Count, Min, Max, Sum, etc. | ADO.NET, C#
Add data (insert into) to the Database using SqlCommand | ADO.NET, C#
Change the data (update) in the Database using SqlCommand | ADO.NET, C#
Delete data in the Database using SqlCommand | ADO.NET, C#
Stored procedures in ADO.NET
Stored procedures in ADO.NET | C#
Transactions in ADO.NET
Transactions in ADO.NET | Database in the C# console application
Reading the picture from the database ADO.NET
Read the image from the database and save it to a file | ADO.NET, C#
Additional topics, questions
Database connection string | ADO.NET, C#

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика