实验一:使用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是安全红线 ...
随机推荐
- LeetCode 561. Array Partition I (C++)
题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...
- Beta阶段基于NABCD评论作品
组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶 刘佳瑞 公冶令鑫 杨磊 刘欣 张宇 卢帝同 一.拉格朗日2018--<飞词> 1.1.NABCD分析 N(Need,需求):该小 ...
- 20135316王剑桥 linux第六周课实验笔记
6.存储器层次结构 6.1存储技术 1.如果你的程序需要的数据是存储在CPU寄存器中的,那么在执行期间,在零个周期内就能访问到它们.如果存储在高速缓冲中,需要1-10个周期.如果存储在主存中,需要50 ...
- 手机端学习助手的说明书需求以及团队PM选择
1.产品的背景 课堂上知识容量大.密度高,学生不能立刻掌握所学知识点,同时,网上资料冗杂繁复,指向性不强,导致学生不能高效的学习,为了充分利用学生的课余时间,培养学生自学能力,辅助老师教学,我们小组希 ...
- 《找出1到正整数N中出现1的次数》
<找出1到正整数N中出现1的次数> 编程思想:依次求出正整数每个位数上出现1的次数,累加即可得到最后想要的结果:而每一位上出现1的个数与和它相邻的其它位数上的数字有关系(以此位置上的数为对 ...
- Scanner的例子
package com.firstDay.one; import java.util.Scanner; public class Information { /** * @param args */ ...
- Anaconda 下libsvm的安装
方法一. 利用VS生成动态库的安装 详细可参考这篇博文进行操作:https://blog.csdn.net/jeryjeryjery/article/details/72628255 方法二. ...
- nginx使用“sudo service nginx start”启动报错解决方案
下载nginx的启动脚本: # wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh 将脚本添加到init.d目录和 ...
- C++派生类构造函数调用顺序(详解)
我们来看下面一段代码: class B1 { public: B1(int i) {cout<<"constructing B1 "<<i<<e ...
- GDI+缩放图片
uses WinAPI.GDIPAPI, WinAPI.GDIPOBJ; var Bitmap1: TGPBitmap; Bitmap2: TBitmap; Graphic: TGPGraphi ...