ExecuteScalar 方法

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
使用 ExecuteScalar 方法从数据库中检索单个值(例如一个聚合值)。与使用 ExecuteReader 方法,然后使用 SqlDataReader 返回的数据执行生成单个值所需的操作相比,此操作需要的代码较少。

典型的 ExecuteScalar 查询可以采用类似于下面的 C# 示例的格式:

CommandText = "select count(*) as NumberOfRegions from region";

Int count = (int) ExecuteScalar();
例子:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="vb" runat="server">
    Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            Dim dtStartDate As DateTime = DateTime.Now.AddDays(-7)
            Dim dtEndDate As DateTime = DateTime.Now.AddDays(-1)
            startdate.Text = dtStartDate.ToShortDateString
            enddate.Text = dtEndDate.ToShortDateString
            literal1.Text = GetEditionNumber()
        End If

    End Sub

    Function GetEditionNumber()
        Dim conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN"))
        Dim cmd As SqlCommand = New SqlCommand("SELECT MAX(Id) from Table", conn)
        conn.Open()
        Dim id As String = cmd.ExecuteScalar
        conn.Close()
        Return id
    End Function
</script>

<html>
    <head></head>
    <body>
        <form runat="server">
            <asp:literal id="literal1" runat="server" /><br>
            <asp:TextBox id="startDate"  runat="server"></asp:TextBox><br>
            <asp:TextBox id="endDate"     runat="server"></asp:TextBox>

        </form>
    </body>
</html>Web.Config used in Example<?xml version="1.0" encoding="utf-8" ?>
<configuration>    
    <appSettings>
        <add key="DSN" value="server=localhost;uid=sa;pwd=;Database=pubs"/>
    </appSettings>
</configuration>

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: .net
上一篇一个程序下载的管理程序(三)
下一篇计数器详细设计
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭