E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

ExecuteScalar
by Charles Carroll

The convenient part about executescalar is its elegance and speed. Since it does not know how to read columns or rows it has much less overhead than a Datareader or DataTable.

ExecuteScalar is a fast way to fetch one value. Here is OLEDB example

   filename=/experiments/executescalar/executescalaroledb.aspx

<Test Script Below>


<%@ Import Namespace="System.Data.OLEDB" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    Dim strConn as string ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";" 
    Dim strSQL as string ="select count(*) from publishers where state='NY'"
    Dim Conn as New OLEDBConnection(strConn)
    Dim Cmd as New OLEDBCommand(strSQL,Conn)
    Conn.Open()
    litHowMany.text=cmd.executescalar()
    Conn.Close()
End Sub
</script>
<html><head>
<title>Grid of New York Data</title>
</head>
<body bgcolor="#FFFFFF">
<font face="Verdana"><h3>New York Count</h3></font>
New York has <ASP:Literal id="litHowMany" runat="server"/> records
</body></html>
Here is SQLserver example.
   filename=/experiments/executescalar/executescalarsqlclient.aspx

<Test Script Below>


<%@ Import Namespace="System.Data.sqlclient" %>
<%@ Import Namespace="System.Configuration.ConfigurationSettings" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    Dim strConn as string =AppSettings("LearnaspSamples")
    Dim strSQL as string ="select count(*) from publishers where state='NY'"
    Dim Conn as New SQLConnection(strConn)
    Dim Cmd as New SQLCommand(strSQL,Conn)
    Conn.Open()
    litHowMany.text=cmd.executescalar()
    Conn.Close()
End Sub
</script>
<html><head>
<title>Grid of New York Data</title>
</head>
<body bgcolor="#FFFFFF">
<font face="Verdana"><h3>New York Count</h3></font>
New York has <ASP:Literal id="litHowMany" runat="server"/> records
</body></html>
Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site