第一步:创建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();
prod.Name = dr.GetString();
prod.Description = dr.GetString();
prod.Category = dr.GetString();
prod.Price = dr.GetDecimal();
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>

第七步:运行代码。

实验一:使用ADO.NET方式读数据的更多相关文章

  1. ADO.NET基础、数据增删改查

    ADO.NET:数据访问技术,就是将C#和MSSQL连接起来的一个纽带.我们可以通过ADO.NET将内存中的临时数据写入到数据库中,也可以将数据库中的数据提取到内存中供程序调用. 数据库数据的增.删. ...

  2. C#向文件写、读数据

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Redis学习——Redis持久化之AOF备份方式保存数据

    新技术的出现一定是在老技术的基础之上,并且完善了老技术的某一些不足的地方,新技术和老技术就如同JAVA中的继承关系.子类(新技术)比父类(老技术)更加的强大! 在前面介绍了Redis学习--Redis ...

  4. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  5. 使用VS2013操作MYSQL8 (ADO.NET方式 & EF6)

    今天有时间测试了一下通过.net环境操作MYSQL数据库,测试过程及结果记录如下: 1.MYSQL安装 (1)我是从MYSQL官网下载的最新版,即MYSQL8.0,在MySql官网的下载页面,找到“M ...

  6. Redis 通过 RDB 方式进行数据备份与还原

    Redis 通过 RDB 方式进行数据备份与还原 Intro 有的时候我们需要对 Redis 的数据进行迁移,今天介绍一下通过 RDB(快照)文件进行 Redis 数据的备份和还原 Redis 持久化 ...

  7. android 之HttpURLConnection的post,get方式请求数据

    get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8081//servlet/LoginServlet" get:http://xxx: ...

  8. Android 采用post方式提交数据到服务器

    接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...

  9. Google判断广告点击作弊的几种方式和数据

     Google判断广告点击作弊的几种方式和数据. 作弊广告点击的CTR数据太高网上有研究说如果CTR值大于了10%的站被干掉的可能性很高,他们会被单独拿出来分析.一般来说低于6-7%的CTR是安全红线 ...

随机推荐

  1. PHP中你应该知道的require()文件包含的正确用法

    以前看一些PHP框架源码的时候,很奇怪在文件包含的时候,会用dirname(__FILE__)来拼凑文件路径,不知道这样做有什么好处,后来终于发现了其中的缘由. 我们来看一个简单的例子: 有a,b,c ...

  2. Python基础_eval(),exec(),globals(),locals(),compile()

    转发:http://www.cnblogs.com/yyds/p/6276746.html 1. eval函数 函数的作用: 计算指定表达式的值.也就是说它要执行的Python代码只能是单个运算表达式 ...

  3. jenkins部署时遇到“似乎无法联网”,导致无法安装默认插件的解决方案

    jenkins安装更新时,默认会检查网络连接,而默认的checkulr 是http://www.google.com/ ,国内是无法访问的,所以修改成任意可以访问的地址即可,比如http://www. ...

  4. servlet 和 threadlocal 与 web容器(理解threadlocal)

    同步机制采用了“以时间换空间”的方式,提供一份变量,让不同的线程排队访问.而ThreadLocal采用了“以空间换时间”的方式,为每一个线程都提供了一份变量的副本,从而实现同时访问而互不影响. htt ...

  5. web.xml配置文件中<async-supported>true</async-supported>报错

    web.xml配置文件中<async-supported>true</async-supported>报错 http://blog.csdn.net/dream_ll/arti ...

  6. 剑指offer :跳台阶

    这题之前刷leetcode也遇到过,感觉是跟斐波拉契差不多的题. 题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 解 ...

  7. HDU 4026 Unlock the Cell Phone 状压dp(类似TSP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4026 Unlock the Cell Phone Time Limit: 6000/3000 MS ...

  8. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

  9. Monty Hall 问题与贝叶斯定理的理解

     三门问题(Monty Hall problem),是一个源自博弈论的数学游戏问题,大致出自美国的电视游戏节目Let's Make a Deal.问题的名字来自该节目的主持人蒙提·霍尔(Monty H ...

  10. lintcode-513-完美平方

    513-完美平方 给一个正整数 n, 找到若干个完全平方数(比如1, 4, 9, ... )使得他们的和等于 n.你需要让平方数的个数最少. 样例 给出 n = 12, 返回 3 因为 12 = 4 ...