CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  C# ADO.NET OleDbCommand - ExecuteScalar

The ExecuteScalar() in C# OleDbCommand Object uses to retrieve a single value from the Database after the execution of SQL Statements or Stored Procedures.

The ExecuteScalar() executes SQL statements or Stored Procedure and returned a scalar value on first column of first row in the returned Result Set. If the Result Set contains more than one columns or rows , it will return only the first column of the first row value and ignore all other values . If the Result Set is empty it will return a NULL reference.

  Int32 count = Convert.ToInt32(cmd.ExecuteScalar());

It is very useful to use with aggregate functions like Count(*) or Sum() etc and also when compare to ExecuteReader() , ExecuteScalar() uses fewer System resources.

         C# Source Code Download           Print Source Code
         C# ADO.NET OleDbCommand - ExecuteScalar - Download
        
C# Tutorial

using System;
using System.Windows.Forms;
using System.Data.OleDb; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            OleDbConnection cnn ;
            OleDbCommand cmd ;
            string sql = null;

            connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
            sql = "Your SQL Statement Here like Select Count(*) from product"; 

            cnn = new OleDbConnection(connetionString);
            try
            {
                cnn.Open();
                cmd = new OleDbCommand(sql, cnn);
                Int32 count = Convert.ToInt32(cmd.ExecuteScalar());
                cmd.Dispose();
                cnn.Close();
                MessageBox.Show (" No of Rows " + count);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}



C# ADO.NET data Providers Related Contents
*     C# ADO.NET Connection
*     C# SQl Server Connection
*     C# OLEDB Connection
*     C# ODBC Connection
*     C# ADO.NET Command
*     C# ADO.NET SqlCommand - ExecuteNonQuery
*     C# ADO.NET OleDbCommand - ExecuteNonQuery
*     C# ADO.NET SqlCommand - ExecuteScalar
*     C# ADO.NET SqlCommand - ExecuteReader
*     C# ADO.NET OleDbCommand - ExecuteReader
*     C# ADO.NET DataReader
*     C# ADO.NET SqlDataReader
*     C# ADO.NET OleDbDataReader
*     C# Multiple Result Sets
*     C# Table Schema from SqlDataReader
*     C# Table Schema from OleDbDataReader
*     C# ADO.NET DataAdapter
*     C# ADO.NET SqlDataAdapter
*     C# ADO.NET OleDbDataAdapter
*     C# ExecuteReader and ExecuteNonQuery


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.