zoukankan      html  css  js  c++  java
  • 实验一:使用ADO.NET方式读数据

    第一步:创建Asp.net应用程序

    在VS中,点击文件->新建->项目,按如图方式选择并输入:

    第二步:新建产品浏览网页窗体Listing.aspx:

    在项目SportsStoreEx上点击右键,选中”添加“->”添加Web窗体“:

    第三步:添加数据库

    先点击下载SportStore数据库脚本,打开Sql Sever Managment Studio,登陆数据库服务器,在SSMS中打开SportStore数据库脚本,点击SSMS工具栏上的红色感叹号,运行脚本,SportStore数据库即建成,并有数据表Products,表中也有数据了。

    第四步:在项目SportStoreEx中添加数据模型类Product:

    右键点击项目SportStore,选中添加->类,输入类名:Product。

    类Product用来描述数据库中的Products表中的记录,类代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace SportStoreEx
    {
        public class Product
        {
            public int ProductID { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
    
            public string Category { get; set; }
            public decimal Price { get; set; }
        }
    }

    第五步:添加GetProducts()方法

    双击Listing.aspx.cs文件,在Listing类里添加GetProducts()方法,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace SportStoreEx
    {
        public partial class Listing : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected IEnumerable<Product> GetProducts()
            {
                IList<Product> products = new List<Product>();
    
                string sql = "select ProductID,Name,Description,Category,Price from Products";
                var con = new SqlConnection("Data Source=.;Initial Catalog=SportsStore;Integrated Security=True");
                var cmd = new SqlCommand(sql, con);
                SqlDataReader dr;
    
                con.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var prod = new Product();
                    prod.ProductID = dr.GetInt32(0);
                    prod.Name = dr.GetString(1);
                    prod.Description = dr.GetString(2);
                    prod.Category = dr.GetString(3);
                    prod.Price = dr.GetDecimal(4);
                    products.Add(prod);
                }
                return products;
            }
        }
    }

    第六步:修改Listing.aspx文件:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Listing.aspx.cs" Inherits="SportStoreEx.Listing" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <%foreach (SportStoreEx.Product prod in GetProducts())
            {
                Response.Write("<div class='item'>");
                Response.Write(string.Format("<h3>{0}</h3>",prod.Name));
                Response.Write("</div>");             
            }
            %>    
        </div>
        </form>
    </body>
    </html>

    第七步:运行代码。

     

  • 相关阅读:
    atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结
    atitit.php 流行框架 前三甲为:Laravel、Phalcon、Symfony2 attilax 总结
    Atitit.执行cmd 命令行 php
    Atitit. 图像处理jpg图片的压缩 清理垃圾图片 java版本
    atitit。企业组织与软件工程的策略 战略 趋势 原则 attilax 大总结
    atitit. 管理哲学 大毁灭 如何防止企业的自我毁灭
    Atitit.java的浏览器插件技术 Applet japplet attilax总结
    Atitit.jquery 版本新特性attilax总结
    Atitit. 软件开发中的管理哲学一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向
    (转)获取手机的IMEI号
  • 原文地址:https://www.cnblogs.com/bayes/p/6025198.html
Copyright © 2011-2022 走看看