【问题标题】:Make a list with no formatting in ASP and C#在 ASP 和 C# 中创建一个没有格式的列表
【发布时间】:2016-04-17 05:45:23
【问题描述】:

我正在尝试使用 C# 进行最简单的查询并将它们列在 ASP.NET 中。我对此感到有些困惑。我熟悉 C#,但不确定如何在 ASP.NET 中列出它们,因为我刚刚开始使用这种语言。

没错,我有一张名为“食谱”的表格。我正在尝试检索 Recipe_ID 和 Name 并列出它们。一个低于另一个。没有格式化。

1 item1
2 item2
3 item3
etc...

任何帮助都会很棒。我为这个愚蠢的问题道歉。我只是可能没有研究正确的东西。

C#

   private void loadRecipe()
    {

        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
        con.Open();
        try
        {
            //Fetching top recipe     
            string query = ("SELECT Recipe_ID, Recipe_Name FROM Recipe");
            SqlCommand cmd = new SqlCommand(query, con);


           [[[what goes here]]]


        }
        catch (Exception)
        {



        }

        con.Close();
    }

ASP.NET

[[[What do i need for this]]]

【问题讨论】:

标签: c# asp.net


【解决方案1】:

欢迎加入。每个人都从某个地方开始。有很多更好的方法可以做到这一点 - 这本质上是一种 hack,将所有内容加载到字符串中并填充标签。但是,它将向您展示如何访问数据。看ASP GridView

C#

private void loadRecipe()
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
    try
    {
        //Fetching top recipe     
        string query = ("SELECT * Recipe_ID, Recipe_Name FROM Recipe");
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.Connection = conn;
        cmd.Connection.Open();
        String body;
        SqlDataReader dr = cmd.ExecuteReader();
        int count = 0;
        if(dr.HasRows){
            while(dr.Read()){
                body += count++ + " " + dr["Body"].ToString() + "<br />";  
          }
        }
        myLabel.Text = body;
    }
    catch (Exception){}
    conn.Close();
}

ASP.NET

<asp:Label runat="server" ID="myLabel" Text=""></asp:Label>

【讨论】:

    【解决方案2】:

    首先重写LoadRecipe方法:

        private DataTable LoadRecipe()
        {
           DataTable recipe = new DataTable();
           var connectionString = @"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True"
    
            using(var con = new SqlConnection(connectionString){
             con.Open();
              try       
                {
                  //Fetching top recipe     
                    var query = ("SELECT Recipe_ID, Recipe_Name FROM Recipe");
                    var cmd = new SqlCommand(query, con);
                    SqlDataReader dr = cmd.ExecuteReader();
                    recipe.Load(dr);
        }
          catch (Exception ex)
               {
                 throw; //(or logg)
               }
          finally // ensure that connection would be closed at any case
               {            
                  con.Close();
               }
          }
            return recipe;
            }
    

    在您的控制器中 (如果您使用 ASP MVC)

    public ActionResult Index()
      {
        var recepy = LoadRecipe();
        return View(recepy); //passing the DataTable to the View
      }
    

    在你身上查看

    @model System.Data.DataTable
    <table border="1">
        <thead>
            <tr>
                <%foreach (System.Data.DataColumn col in Model.Columns) { %>
                    <th><%=col.Caption %></th>
                <%} %>
            </tr>
        </thead>
        <tbody>
        <% foreach(System.Data.DataRow row in Model.Rows) { %>
            <tr>
                <% foreach (var cell in row.ItemArray) {%>
                <td><%=cell.ToString() %></td>
                <%} %>
            </tr>
        <%} %>         
        </tbody>
    </table>
    

    这会将 DataTable 传递给视图并将它们表示为表格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 2017-10-14
      • 2013-05-02
      • 1970-01-01
      • 2021-11-30
      • 2015-05-21
      • 1970-01-01
      相关资源
      最近更新 更多