dsc56927 2011-02-14 04:24
浏览 41
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • duanlinpi0265 2011-02-14 05:03
    关注

    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. ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测