sql 查询中的多个值显示在 aspx 页面上

Multiple values from sql query display on aspx page

我正在尝试将来自多查询 sql 的数据显示到我的 aspx 页面,但没有得到结果。

我有一个母版页,我想在其中显示来自 sql 的值。

我的主页(Site.Master):

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="MySite.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
    <head runat="server"></head>
    <body>
        <form runat="server">
            <div class="content">
                <div>
                    <font class="label label-primary"><%= DateTime.Now.ToShortDateString() %>
                    <asp:Label runat="server" ID="LabelCountMember"></asp:Label>
                    <asp:Label runat="server" ID="LabelCountLogins"></asp:Label>
                    <asp:Label runat="server" ID="LabelCountFailedLogins"></asp:Label>
                </div>
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
                <hr />
                <footer>
                    <p>This is my footer!</p>
                </footer>
            </div>
        </form>
    </body>
</html>

我的代码隐藏文件 (Site.Master.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

namespace MySite
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void countmembers(object sender, EventArgs e)
        {
            string sql = @"SELECT (SELECT COUNT(MemberID) from MyTable where Mamber = 'Registered') AS AllRegMembers,
                                        (SELECT COUNT(MemberID) from MyTable where LoginStatus = 'ok') AS AllLogins,
                                        (SELECT COUNT(MemberID) from MyTable where LoginStatus = 'fail') AS AllFailLogins";

            string variable;
            using (var connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
            using (var command = new SqlCommand(sql, connection))
            {
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    //Check the reader has data:
                    if (reader.Read())
                    {
                        variable = reader.GetInt32(reader.GetOrdinal("AllRegMembers")).ToString();

                    }
                    // If you need to use all rows returned use a loop:
                    while (reader.Read())
                    {
                        // Do something
                        string strAllRegMembers = reader.GetInt32(reader.GetOrdinal("AllRegMembers")).ToString();
                        string strAllLogins = reader.GetInt32(reader.GetOrdinal("AllLogins")).ToString();
                        string strAllFailLogins = reader.GetInt32(reader.GetOrdinal("AllFailLogins")).ToString();

                        Label.LabelCountMember.Text = strAllRegMembers;
                        Label.LabelCountLogins.Text = strAllLogins;
                        Label.LabelCountFailedLogins.Text = strAllFailLogins;
                    }
                }
            }
        }
    }
}

请帮我梳理一下基本结构。我尝试了很多不同的方法,但 none 有效,而且在处理来自 reader 的多个值时,我真的找不到一个简单的例子。我显然在搜索错误的关键字。

我可以通过使用以下代码让它在 Classic ASP 中工作,但我真的很想开始转向 C#。

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="dbcon.asp" -->
<%
sql = "SELECT (SELECT COUNT(MemberID) from MyTable where Mamber = 'Registered') AS AllRegMembers, " & _
                "SELECT COUNT(MemberID) from MyTable where LoginStatus = 'ok') AS AllLogins, " & _
                "(SELECT COUNT(MemberID) from MyTable where LoginStatus = 'fail') AS AllFailLogins";"

call dbOpen(conn)

Set rs = conn.Execute(sql)
strAllRegMembers = rs("AllRegMembers")
strAllLogins = rs("AllLogins")
strAllFailLogins = rs("AllFailLogins")
rs.Close : Set rs = Nothing

Call dbClose(conn)
%>
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<span>Registered members: <%= strAllRegMembers %></span>
<span>Number of OK logins: <%= strAllLogins %></span>
<span>Number of failed logins: <%= strAllFailLogins %></span>
</body>
</html>

感谢 Chris 和 Jo Biesta,下面是一个有效的代码隐藏文件。完美的家伙,谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

namespace MySite
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            countmembers();
        }

        public void countmembers()
        {

            string strAllRegMembers = String.Empty;
            string strAllLogins = String.Empty;
            string strAllFailLogins = String.Empty;                 
            string sql = @"SELECT (SELECT COUNT(MemberID) from MyTable where Mamber = 'Registered') AS AllRegMembers,
                                        (SELECT COUNT(MemberID) from MyTable where LoginStatus = 'ok') AS AllLogins,
                                        (SELECT COUNT(MemberID) from MyTable where LoginStatus = 'fail') AS AllFailLogins";

            string variable;
            using (var connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
            using (var command = new SqlCommand(sql, connection))
            {
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    //Check the reader has data:
                    if (reader.Read())
                    {
                        variable = reader.GetInt32(reader.GetOrdinal("AllRegMembers")).ToString();

                    }
                    // If you need to use all rows returned use a loop:
                    while (reader.Read())
                    {
                        // Do something
                        strAllRegMembers = reader.GetInt32(reader.GetOrdinal("AllRegMembers")).ToString();
                        strAllLogins = reader.GetInt32(reader.GetOrdinal("AllLogins")).ToString();
                        strAllFailLogins = reader.GetInt32(reader.GetOrdinal("AllFailLogins")).ToString();


                    }
                }
            }

               LabelCountMember.Text = strAllRegMembers;
               LabelCountLogins.Text = strAllLogins;
               LabelCountFailedLogins.Text = strAllFailLogins;
        }
    }
}

对于可能觉得这有用的其他人。如果您 return 值是文本格式,请改用此格式:

reader.GetString(reader.GetOrdinal("AllRegMembers"));

除非我遗漏了什么(阅读我 phone 上的代码有点棘手)看起来你的问题是没有任何东西在调用你的 countmembers 方法。

尝试从页面加载方法调用该方法,看看进展如何。

同样按照 Chris 的建议,在您的代码中放置一个调试点并单步执行它。然后你会看到它不起作用的地方。

试试下面的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Web.Configuration;
    using System.Data;

    namespace MySite
    {
        public partial class SiteMaster : MasterPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                countmembers();
            }

            public void countmembers()
            {

                string strAllRegMembers = String.Empty;
                string strAllLogins = String.Empty;
                string strAllFailLogins = String.Empty;                 
                string sql = @"SELECT (SELECT COUNT(MemberID) from MyTable where Mamber = 'Registered') AS AllRegMembers,
                                            (SELECT COUNT(MemberID) from MyTable where LoginStatus = 'ok') AS AllLogins,
                                            (SELECT COUNT(MemberID) from MyTable where LoginStatus = 'fail') AS AllFailLogins";

                string variable;
                using (var connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
                using (var command = new SqlCommand(sql, connection))
                {
                    connection.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        //Check the reader has data:
                        if (reader.Read())
                        {
                            variable = reader.GetInt32(reader.GetOrdinal("AllRegMembers")).ToString();

                        }
                        // If you need to use all rows returned use a loop:
                        while (reader.Read())
                        {
                            // Do something
                            strAllRegMembers = reader.GetInt32(reader.GetOrdinal("AllRegMembers")).ToString();
                            strAllLogins = reader.GetInt32(reader.GetOrdinal("AllLogins")).ToString();
                            strAllFailLogins = reader.GetInt32(reader.GetOrdinal("AllFailLogins")).ToString();


                        }
                    }
                }

                   Label.LabelCountMember.Text = strAllRegMembers;
                            Label.LabelCountLogins.Text = strAllLogins;
                            Label.LabelCountFailedLogins.Text = strAllFailLogins;
            }
        }
    }