实验一:使用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();
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方式读数据的更多相关文章
- ADO.NET基础、数据增删改查
ADO.NET:数据访问技术,就是将C#和MSSQL连接起来的一个纽带.我们可以通过ADO.NET将内存中的临时数据写入到数据库中,也可以将数据库中的数据提取到内存中供程序调用. 数据库数据的增.删. ...
- C#向文件写、读数据
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Redis学习——Redis持久化之AOF备份方式保存数据
新技术的出现一定是在老技术的基础之上,并且完善了老技术的某一些不足的地方,新技术和老技术就如同JAVA中的继承关系.子类(新技术)比父类(老技术)更加的强大! 在前面介绍了Redis学习--Redis ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- 使用VS2013操作MYSQL8 (ADO.NET方式 & EF6)
今天有时间测试了一下通过.net环境操作MYSQL数据库,测试过程及结果记录如下: 1.MYSQL安装 (1)我是从MYSQL官网下载的最新版,即MYSQL8.0,在MySql官网的下载页面,找到“M ...
- Redis 通过 RDB 方式进行数据备份与还原
Redis 通过 RDB 方式进行数据备份与还原 Intro 有的时候我们需要对 Redis 的数据进行迁移,今天介绍一下通过 RDB(快照)文件进行 Redis 数据的备份和还原 Redis 持久化 ...
- android 之HttpURLConnection的post,get方式请求数据
get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8081//servlet/LoginServlet" get:http://xxx: ...
- Android 采用post方式提交数据到服务器
接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...
- Google判断广告点击作弊的几种方式和数据
Google判断广告点击作弊的几种方式和数据. 作弊广告点击的CTR数据太高网上有研究说如果CTR值大于了10%的站被干掉的可能性很高,他们会被单独拿出来分析.一般来说低于6-7%的CTR是安全红线 ...
随机推荐
- 2-Second Scrum Meeting-20151202
任务安排 闫昊: 今日完成:设计学习进度的管理. 明日任务:请假.(编译+计组,压力有点大) 金哉仁: 今日完成:继续商讨APP相关界面与设计,安装AndroidStudio. 明日任务:请假.(编译 ...
- java怎样把后台值传到前台
后台代码:if(this.Request["type"]!=null){aaa=this.Request["type"].ToString(); try{bbb ...
- Beta Scrum Day 7 — 听说
7#听说
- SpringMVC(二)-- springmvc的系统学习之跳转结果的方式
资源: 尚学堂 邹波 springmvc框架视频 若无特别注明,例子项目的配置方式为注解 一.设置ModelAndView对象. 1.根据View的名称和视图解析器跳转到指定的页面. 2.跳转的 ...
- Android笔记-4-实现登陆页面并跳转和简单的注册页面
实现登陆页面并跳转和简单的注册页面 首先我们来看看布局的xml代码 login.xml <span style="font-family:Arial;font-size:18px; ...
- 18_集合框架_第18天_集合、Iterator迭代器、增强for循环 、泛型_讲义
今日内容介绍 1.集合 2.Iterator迭代器 3.增强for循环 4.泛型 01集合使用的回顾 *A:集合使用的回顾 *a.ArrayList集合存储5个int类型元素 public stati ...
- js作用域相关笔记
1.js引擎.编译器.作用域. 引擎:负责JS全过程的编译和执行: 编译器:负责语法分析和代码生成: 作用域:负责收集并维护声明组成的查询,以及当前执行代码对这些变量的访问权限(简言之,作用域就是用于 ...
- 继《在Windows下编译扩展OpenCV 3.1.0 + opencv_contrib》修订
在之前的<在Windows下编译扩展OpenCV 3.1.0 + opencv_contrib>中有些问题,后来由于时间不是很充足,故现在对其中的问题进行完善,如下所示对红色框框中的相应的 ...
- 014 C语言文法定义与C程序的推导过程
- [转帖]SQLSERVER2008R2 将于2019.7.9 结束支持服务 Windows server 2008r2 将于 2020.1.14 结束支持
来源: https://cloudblogs.microsoft.com/sqlserver/2018/07/12/sql-server-2008-end-of-support-is-the-firs ...