【问题标题】:How to display stored procedures output in html table?如何在 html 表中显示存储过程输出?
【发布时间】:2013-01-12 20:59:06
【问题描述】:

我想在 HTML 表格中显示存储过程的输出。我知道如何在 gridview 中显示,但我不知道如何在 HTML 表格中显示。谁能给我建议或提供示例代码,以便我从中学习。我已经搜索并尝试解决。我想从创建的 HTML 表中创建图表。

【问题讨论】:

  • 一点也不!!!这个问题和我的问题完全不同。我创建了自己的存储过程,它执行诸如基于不同标准显示数据之类的操作。我已经在 asp.net 的 GridView 中显示了这些存储过程的输出,但现在我希望该存储过程的输出应该显示在 HTML 表中。我希望 HTML 表根据存储过程输出动态创建。
  • 你在使用asp.net mvc吗? ado.net?向我们提供有关您尝试了什么的更多详细信息!
  • @Felipe Oriani,我正在使用 asp.net MVC3 和 SQL Server 2008。
  • 好的,但是,你试过了吗?如果你这样做了,请发布一些代码!

标签: c# asp.net-mvc sql-server-2008 stored-procedures html-table


【解决方案1】:

首先,您必须创建一个 ViewModel 来显示您想在视图上列出哪些信息。像这样的:

public class ProductViewModel 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    /* other properties you need */
}

在您的控制器中,您可以访问数据库并创建一个列表并将其返回到显示在 html 输出中的视图。 我可以建议您直接在控制器中使用 Ado.Net 创建数据访问层。

public ActionResult Index()
{
    var model = new List<ProductViewModel>();

    // create a connection
    SqlConnection con = new SqlConnection("your_connection_string");    
    try 
    {
        // open the conneciton
        con.Open();

        // prepare a command
        SqlCommand command = new SqlCommand("You_Stored_Procedure_Name",con);   
        command.CommandType = CommandType.StoredProcedure;

        // add parameters if you need
        // command.Parameters.AddWithValue("ParameterName", value);

        // execute a reader with the command
        using (var reader = command.ExecuteReader())
        {
            // loop in the result and fill the list
            while (reader.Read())
            {
                // add items in the list
                model.Add(new ProductViewModel() 
                        {
                            Id = (int)reader["Id"],
                            Name = reader["Name"].ToString(),
                            Price = (decimal)reader["Price"]
                            // other properties
                        });
            }
        }       
    }
    catch 
    {
    }
    finally 
    {   
        con.Close();
    }

    return View(model);
}

在您的视图中,您可以使用 IEnumrable&lt;ProductViewModel&gt; 键入它并将其列在 html 表格或您需要的任何 html 结构中,如下所示:

@model IEnumerable<ProductViewModel>

<table>
    <tr>
        <th>Name</th>
        <th>Price</th>      
    </tr>
    @foreach(var item in Model)
    {
        <tr>
            <td>@item.Name</td>
            <td>@item.Price.ToString("C2")</td>
        </tr>
    }
</table>

【讨论】:

    【解决方案2】:

    我认为...您可以调用 SP 并在您的 .CS 页面上获取数据...

    现在你必须做一件事.. 将该 Datareader 存储在任何列表中。并应用 linq 查询,以便您可以根据需要获取记录。将该记录存储在单独的变量中。

    参见示例:

     MainHTML = MainHTML + "<div class=" + "HideDivClass" + " id=" + TMid + "><table width=" + "180" + " cellspacing=" + "1" + " cellpadding=" + "2" + " align=" + "left" + "><tbody><tr><td style=" + "text-align:center;font-size:13px" + "align=" + "center" + " colspan=" + "2" + "><b>" + TransModeName + "</b></td></tr><tr><td class=" + "DetailsFontSize" + ">Last 7 days</td><td  class=" + "DetailsFontSize" + " " + "align=" + "right" + ">$" + TotalSumActualCharge7Days + "</td></tr><tr></tr><tr><td class=" + "DetailsFontSize" + ">Last 30 days</td><td class=" + "DetailsFontSize" + " " + "align=" + "right" + ">$" + TotalSumActualCharge30Days + "</td></tr><tr><td class=" + "DetailsFontSize" + ">This Year</td><td class=" + "DetailsFontSize" + " " + "align=" + "right" + ">$" + TotalSumActualChargeThisYear + "</td></tr><tr><td class=" + "DetailsFontSize" + ">Total</td><td class=" + "DetailsFontSize" + " " + "align=" + "right" + ">$" + TotalSumActualChargeTotal + "</td></tr><tr><td class=" + "DetailsFontSize" + ">Avg/Shipment</td><td class=" + "DetailsFontSize" + " " + "align=" + "right" + ">$" + AvgShip.ToString("#.####") + "</td></tr><tr><td class=" + "DetailsFontSize" + "align=" + "center" + " colspan=" + "2" + "></td></tr></tbody></table></div>";
    

    这里我将 MainHTML 作为一个字符串...并制作了一个字符串(HTML 布局/设计格式)并将这个字符串绑定到标签。

    喜欢。 label.text = MainHTML.tostring(); 您可以根据自己的喜好进行设计...

    【讨论】:

    • 哦,我会试试这个。太感谢了。你有任何关于这个的示例代码。我是新来的,所以我问了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多