php 到 asp.net:如何在视图中显示 sql 结果

发布于 2024-10-17 09:24:19 字数 764 浏览 1 评论 0原文

首先,我是一名 PHP 开发人员,试图了解 ASP.NET。

所以我创建了一个基本的 MVC 项目。

我有一个没有已知字段的查询(即从产品中选择 *)我该如何:

  1. 在控制器中执行 - 我的尝试:

    public ActionResult getProducts() 
    {
    
        使用 (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ToString()))
        {
            string sql = "从产品中选择*";
    
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);              
        }
    
        返回视图();  
    }
    
  2. 如何将结果传递给视图,然后循环遍历它们,如下所示:

    foreach ($data as $key => $val) 
    {
        回显 $key。 = '.$val.'
    '; }

请帮忙,因为这在 PHP 中非常简单,但在 ASP.NET 中似乎非常令人困惑。

附:抱歉格式错误。

干杯, 旅行。

Firstly, im a PHP developer trying to get my head around asp.net.

so i have created a basic MVC project.

I have a query without the fields known (ie. select * from products) how do I:

  1. execute in Controller - my attempt:

    public ActionResult getProducts() 
    {
    
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ToString()))
        {
            string sql = "select * from products";
    
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);              
        }
    
        return View();  
    }
    
  2. how do i pass the results to a View and then loop through them like:

    foreach ($data as $key => $val) 
    {
        echo $key.' = '.$val.'<br>';
    }
    

please help as this is SOOOOO simple in PHP but seems to be very confusing in asp.net.

ps. sorry for the formatting.

cheers,
trav.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-10-24 09:24:19

首先,您需要一个模型类:

public class Product
{
    public string Name { get; set; }
    public int Price { get; set; }
    public int Id { get; set; }
} 

在您的控制器中:

public ActionResult getProducts() 
    {

                    var products = new List<Product>();

        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ToString()))
        {
            string sql = "select * from products";

            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

                            //read the results
                            while(rdr.Read() )
                            {
                                //map or hydrate a new product
                                var p = new Product();
                                p.Name = rdr["Name"];
                                p.Id = Int.Parse(rdr["Id"]);
                                p.Price = Int.Parse(rdr["Price"]);

                                //add new product to list we created earlier
                                products.Add( p );
                            }            
        }

        return View(products);  
    }

在视图中:

     //make sure the page inherits from ViewPage<List<Product>>       

     <% foreach( var product in Model ) { %>
          <%= product.Name %>
     <% } %>

一个警告。您在这里做事“困难”,不使用像实体框架或 Nhibernate 这样的 ORM,也不遵循不使用视图模型的最佳实践。当然,这对于学习来说没问题,只是需要注意一些事情。 ;)

First you'll need a model class:

public class Product
{
    public string Name { get; set; }
    public int Price { get; set; }
    public int Id { get; set; }
} 

And in your controller:

public ActionResult getProducts() 
    {

                    var products = new List<Product>();

        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ToString()))
        {
            string sql = "select * from products";

            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

                            //read the results
                            while(rdr.Read() )
                            {
                                //map or hydrate a new product
                                var p = new Product();
                                p.Name = rdr["Name"];
                                p.Id = Int.Parse(rdr["Id"]);
                                p.Price = Int.Parse(rdr["Price"]);

                                //add new product to list we created earlier
                                products.Add( p );
                            }            
        }

        return View(products);  
    }

And in the View:

     //make sure the page inherits from ViewPage<List<Product>>       

     <% foreach( var product in Model ) { %>
          <%= product.Name %>
     <% } %>

One caveat. Your doing things "the hard way" here by not using an ORM like Entity Framework or Nhibernate and not following best practices by not using a View Model. Thats fine for just learning of course, just something to be aware of. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文