|
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
<%@ 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
<%@ 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>
|