【问题标题】:Displaying data that is grouped by an ID显示按 ID 分组的数据
【发布时间】:2021-10-10 11:57:16
【问题描述】:

我正在使用 c#、asp.net、webforms 和 Visual Studio。我也在使用 SQL Server Management Studio 来创建我的存储过程。

如何按名为 RegId 的 ID 进行分组,然后显示每个 RegId 的 Title: URL 列表?

我的表示例:

存储过程:

USE [MyDB]
GO
/****** Object:  StoredProcedure [dbo].[spGetRegSess]    Script Date: 8/5/2021 7:31:49 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spGetRegSess]
       
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT *
    FROM RegSess
END

我更喜欢以文本格式显示的内容:

Bob has Google at http://www.google.com, Yahoo at http://www.yahoo.com, and MSN at http://www.msn.com.

John has Reddit at http://www.reddit.com.

我的 webforms asp.net 页面上有这个按钮:

 <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="Send" />

单击按钮后,我从存储过程中获取数据并将其放入数据表中:

protected void btnSend_Click(object sender, EventArgs e){
    DataTable dataTable = new DataTable();

    //Fill datatable
    using (SqlConnection sqlConn = new 
         SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString))
    {

        sqlConn.Open();

        using (var command = sqlConn.CreateCommand())
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "spGetRegSess";

            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dataTable);
            sqlConn.Close();
            da.Dispose();
            var myCount = dataTable.Rows.Count;
        }
        sqlConn.Close();
    }
}

我在这部分之后被卡住了。我知道我需要按 RegId 进行分组,但不确定执行此操作的语法。

【问题讨论】:

  • 您能分享您的存储过程以及使用数据表的 Web 表单代码吗?
  • @Drewskis 当然!我已经编辑过了。

标签: c# asp.net datatable


【解决方案1】:

您可以使用 XML Path 和 Stuff 来连接结果,如下所示:

SELECT 
  r.RegistrantId, 
  r.Name, 
  STUFF(
    (
      SELECT 
        ', ' + r2.Name + ' has ' + r2.[Whatever_Your_Column_Name_Is_here] + ' at ' + CAST(
          r2.Url AS VARCHAR(MAX)
        ) 
      FROM 
        RegSess r2 
      WHERE 
        (r2.RegistrantId = r.RegistrantId) FOR XML PATH(''), 
        TYPE
    ).value('(./text())[1]', 'VARCHAR(MAX)'), 
    1, 
    2, 
    ''
  ) AS URLs 
FROM 
  RegSess r 
GROUP BY 
  r.RegistrantId, 
  r.Name

如果您希望它按会话分组(在您的情况下为列标题),您只需将其添加到您的分组中,如下所示:

SELECT 
  r.RegistrantId, 
  r.Name, 
  r.Title, 
  STUFF(
    (
      SELECT 
        ', ' + r2.Name + ' has ' + r2.[Whatever_Your_Column_Name_Is_here] + ' at ' + CAST(
          r2.Url AS VARCHAR(MAX)
        ) 
      FROM 
        RegSess r2 
      WHERE 
        (
          r2.RegistrantId = r.RegistrantId 
          and r2.Title = r.Title
        ) FOR XML PATH(''), 
        TYPE
    ).value('(./text())[1]', 'VARCHAR(MAX)'), 
    1, 
    2, 
    ''
  ) AS URLs 
FROM 
  RegSess r 
GROUP BY 
  r.RegistrantId, 
  r.Name, 
  r.Title

感谢@kevin-fairchild 以及他对https://stackoverflow.com/a/273330/8304027的回答

【讨论】:

  • 谢谢。我刚试过这个,它给了我一些奇怪的结果。这是发生了什么:ibb.co/qWJbgT0
  • 这很可能是由于标题是外部分组的一部分。修改了上面的查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多