实验一:使用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";
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SportsStore;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader dr;

            con.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Product prod = new Product
                {
                    ProductID = dr.GetInt32(0),
                    Name = dr.GetString(1),
                    Description = dr.GetString(2),
                    Category = dr.GetString(3),
                    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>

第七步:运行代码。

时间: 2024-08-11 03:23:33

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

ADO.NET 非连接方式进行数据访问

1. 配置 DataAdapter 以检索信息 1.1. DataAdapter ? 数据适配器是数据集与数据源交互的桥梁 –使相当于数据源本地拷贝的数据集可以与数据源之间进行交互. ? 为数据库提供的主要两种数据适配器 – SqlDataAdapter:不经过OLEDB层直接与SQLServer交互,速度较OleDbDataAdapter快. – OleDbDataAdapter:适用于任何可以用OLEDB数据提供者访问的数据源. 1.2. XxxDataAdapter对象模型 ? Comma

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

今天有时间测试了一下通过.net环境操作MYSQL数据库,测试过程及结果记录如下: 1.MYSQL安装 (1)我是从MYSQL官网下载的最新版,即MYSQL8.0,在MySql官网的下载页面,找到“MySQL Installer for Windows”. MySql下载页面地址:https://dev.mysql.com/downloads/ 上图中,MySql for Visual Studio和Connector/NET用于后边的EF操作,MySQL Installer for Windo

苹果微信浏览器不能post方式提交数据问题

form表单中采用post方式提交数据时,在苹果的微信浏览器中无法传递,安卓的可以 如图: 在controller中获取该数据为 null 将表单的提交方式修改为get就能够获取到 现在采用Ajax方式进行提交

get和post方式请求数据,jsonp

get方式请求数据: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 19.0px Consolas; color: #289c97 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 19.0px Consolas; color: #060606 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 19.0px Consolas; color: #4663cc }

JQuery以JSON方式提交数据到服务端

JQuery将Ajax数据请求进行了封装,从而使得该操作实现起来容易许多.以往我们要写很多的代码来实现该功能,现在只需要调用$.ajax()方法,并指明请求的方式.地址.数据类型,以及回调方法等.下面的代码演示了如何将客户端表单数据封装成JSON格式,然后通过JQuery的Ajax请求将数据发送到服务端,并最终将数据存储到数据库中.服务端定义为一个.ashx文件,事实上你可以将服务端定义为任何能接收并处理客户端数据的类型,如Web Service,ASP.NET Page,Handler等. 首

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

接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="m

ADO.NET程序访问数据的组件

组成--数据集(内存中的数据库) --DataSet数据集 --DataTable数据表 --DataColumn数据列 --DataRow数据行 --DataView数据视图--NET数据提供程序 --Connection连接(程序和数据库之间的桥梁) --Open/Close --ConnectionString:连接字符串(目标数据库的信息) --服务器:server / data source --用户名:uid / user id --密码:pwd / password --数据库:d

Android 存储文件方式之一---SharedPreferences 内容提供者,以xml 的方式进行数据 存储。是一种轻量级的文件数据存储

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 //UI界面的布局 文件<br><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"

C#带cookie Post和Get方式发送数据,保持cookie

在实际编程中,可能需要读取特定网页的信息,但很多网站需要用户登录后,才能够获取相关的页面内容,这就需要编程者先临时存储当前的cookie,在C#中可以使用CookieContainer 对象来保存登录后的Cookie信息,这样,在每次发送数据的时候,附加上Cookie信息,就可以了. #region 同步通过POST方式发送数据 /// <summary> /// 通过POST方式发送数据 /// </summary> /// <param name="Url&qu