Vb.Net-Informations.com

  ADO.NET ExecuteScalar in OleDbCommand Object




   Categories

    HOME
    VB.NET
    CSHARP

 
   














ADO.NET ExecuteScalar in OleDbCommand Object

ExecuteScalar() in OleDbCommand Object is used for get a single value from Database after its execution. It executes SQL statements or Stored Procedure and returned a scalar value on first column of first row in the Result Set. If the Result Set contains more than one columns or rows , it takes only the first column of first row, all other values will ignore. If the Result Set is empty it will return a Null reference. It is very useful to use with aggregate functions like Count(*) or Sum() etc. When compare to ExecuteReader() , ExecuteScalar() uses fewer System resources.


         VB.NET Source Code Download           Print Source Code
         ADO.NET ExecuteScalar in OleDbCommand Object - Download
        
VB.Net Tutorial

Imports System.Data.OleDb
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim cnn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim sql As String

        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)
            Dim count As Int32 = Convert.ToInt32(cmd.ExecuteScalar())
            cmd.Dispose()
            cnn.Close()
            MsgBox(" No of Rows " & count)
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class
		


connetionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = Your mdb filename;"

sql = "Your SQL Statement Here like Select Count(*) from product"

You have to replace the string with your realtime variables.

ADO.NET Data Providers Related Contents
*     ADO.NET Connection Object
*     ADO.NET SQL Server Connection
*     ADO.NET OLEDB Connection
*     ADO.NET ODBC Connection
*     ADO.NET Command
*     ADO.NET ExecuteNonQuery in SqlCommand Object
*     ADO.NET ExecuteNonQuery in OleDbCommand Object
*     ADO.NET ExecuteScalar in SqlCommand Object
*     ADO.NET ExecuteReader in SqlCommand Object
*     ADO.NET ExecuteReader in OleDbCommand Object
*     How to ADO.NET DataReader
*     How to ADO.NET SqlDataReader
*     How to ADO.NET OleDbDataReader
*     How to Multiple Result Sets in ADO.NET
*     Getting Schema Informations from SqlDataReader
*     Getting Schema Informations from OleDbDataReader
*     What is DataAdapter
*     What is SqlDataAdapter
*     What is OleDbDataAdapter

VB.Net Related Topics
*     Microsoft .Net Framework Tutorials
*     VB.NET Language Basics Tutorials
*     VB.NET Program Flow Control Tutorials
*     VB.NET Collections Tutorials
*     VB.NET String Tutorials
*     VB.NET Files Tutorials
*     VB.NET Excel 2007 Tutorials
*     VB.NET Crystal Reports Tutorials
*     VB.NET Communications Tutorial
*     VB.NET ADO.NET Tutorial with source code
*     ADO.NET Data Providers help and Tutorial
*     VB.NET ADO.NET Dataset Tutorial
*     ADO.NET DataAdapter and Dataset
*     VB.NET ADO.NET DataView Tutorial
*     VB.NET Remoting Tutorial
*     VB.NET XML Tutorial
Search here for more CSharp Source Code :

  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2010 All Rights Reserved