• ASP.NET MVC:解析 MVC+ADO.NET Entity(实体类)+Oracle


    1,功能描述

    一个基于标准的ASP.NET MVC2.0 + ADO.NET Entity(实体类)+Oracle数据库的一个项目.主要功能有:用户登录,产品的操作,商品展示,添加产品,修改商品,删除商品. 

    2,技术与环境

    操作系统:

    windows

    开发语言:

    C#

    开发框架:

    ASP.NET MVC 2.0

    数据库:

    Oracle

    开发软件:

    Microsoft Visual Studio 2010

     开发技术

     ASP.NET MVC +ADO.NET Entity

    项目组长:

    yuanbo

    成员:

    null

    3,数据库设计

     数据关系图:

    guanxi

    3.1,基本数据库 

    3.1.1 sql-mvc-basic.sql

    View Code
    Microsoft Windows XP [版本 5.1.2600]
    (C) 版权所有 1985-2001 Microsoft Corp.
    
    C:\Documents and Settings\Administrator>sqlplus
    
    SQL*Plus: Release 10.2.0.1.0 - Production on 星期五 3月 1 14:44:29 2013
    
    Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    
    请输入用户名:  system/m123
    
    连接到:
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
    
    SQL> --1,展示当前用户
    SQL> show user;
    USER 为 "SYSTEM"
    SQL> --2,创建"sunshine"用户并给密码"m123"
    SQL> create user sunshine identified by m123;
    
    用户已创建。
    
    SQL> --3,授予该用户“链接”和“资源”俩个权限
    SQL> grant connect,resource to sunshine;
    
    授权成功。
    
    SQL> --4,切换到sunshine用户
    SQL> connect sunshine/m123;
    已连接。
    SQL> --5,展示当前用户
    SQL> show user;
    USER 为 "SUNSHINE"
    
    SQL> --6,创建"用户表"
    SQL> create table Users
      2  (
      3  username varchar(100) primary key,
      4  userpass varchar(100) not null
      5  );
    
    表已创建。
    
    SQL> --7,展示表结构
    SQL> desc Users;
     名称                                      是否为空? 类型
     ----------------------------------------- -------- ----------------------------
    
     USERNAME                                  NOT NULL VARCHAR2(100)
     USERPASS                                  NOT NULL VARCHAR2(100)
    
    SQL> --8,向"用户表"插入一条测试数据
    SQL> insert into Users(username,userpass) values('yb','m123');
    
    已创建 1 行。
    
    SQL> --9,创建"产品表"
    SQL> create table Product
      2  (
      3  productId int primary key,
      4  productName varchar(100),
      5  unitprice number(6,2),
      6  type varchar(100) check(type in('电器','水果'))
      7  );
    
    表已创建。
    
    SQL> --10,为产品表创建一个序列
    SQL> create sequence seqProduct start with 1000 increment by 1;
    
    序列已创建。
    
    SQL> --11,向"产品表"插入一个数据
    SQL> insert into Product(productId,productName,unitprice,type) values(seqProduct
    .nextval,'洗衣机','2000','电器');
    
    已创建 1 行。
    
    SQL> --12,查看产品表数据
    SQL> select * from Product;
    
     PRODUCTID
    ----------
    PRODUCTNAME
    --------------------------------------------------------------------------------
    
     UNITPRICE
    ----------
    TYPE
    --------------------------------------------------------------------------------
    
          1000
    洗衣机
          2000
    电器
    
    
    SQL> --13,提交以上操作
    SQL> commit;
    
    提交完成。
    
    SQL>

    3.2,插入测试数据

    无,在3.1.1已插入测试数据。

    3.3,操作表步骤      

    3.3.1 1, Users.sql 

    View Code
    -- =============================================
    -- ylb_menu: MVC测试数据库
    -- table: 1,Users
    -- remark:对用户表的操作与步骤
    -- author: yuanbo
    -- pubdate:16:05 2013-03-01
    -- =============================================
    connect sunshine/m123
    
    go
    -- =============================================
    -- ylb_test: 1,向"Users"表插入测试数据
    -- remark: 测试数据
    -- =============================================
    insert into Users(username,userpass) values('yb','m123')
    
    go
    -- =============================================
    -- ylb: 1,Login
    -- remark: 用户登录
    -- =============================================
    select COUNT(*) from Users where username='yb' and userpass='m123'

    3.3.2  2, Product.sql

    View Code
    -- =============================================
    -- ylb_menu: MVC测试数据库
    -- table: 1,Products
    -- remark:对产品表的操作与步骤
    -- author: yuanbo
    -- pubdate:16:05 2013-03-01
    -- =============================================
    connect sunshine/m123
    
    go
    -- =============================================
    -- ylb_test: 1,向"Users"表插入测试数据
    -- remark: 测试数据
    -- =============================================
    
    go
    -- =============================================
    -- ylb: 1,GetAll
    -- remark: 获取所有产品,并以productId降序排列
    -- =============================================
    select productId,productName,unitPrice,type from Product order by productId desc
    
    go
    -- =============================================
    -- ylb: 2,Add
    -- remark: 添加一个产品
    -- field: productName,unitPrice,type
    -- =============================================
    select productName,unitPrice,type from Product
    go
    insert into Product(productId,productName,unitPrice,type) values(seqProduct.nextval,,,)
    
    go
    -- =============================================
    -- ylb: 3,GetModel
    -- remark: 获得一个实体对象,根据productId
    -- =============================================
    select productId,productName,unitPrice,type from Product where productId=0
    
    go
    -- =============================================
    -- ylb: 4,Update
    -- remark: 修改一条信息 ,根据productId
    -- =============================================
    update Product set productName='yb',unitPrice='2.3',type='电器' where productId=0
    
    go
    -- =============================================
    -- ylb: 5,Delete
    -- remark: 删除一条信息,根据productId
    -- =============================================
    delete Product where productId=0
    4,功能截图

     4.1,前台

    4.1.1 用户登录(/Views/Account/Login.aspx)

    login
    4.1.2 商品展示(/Views/Product/Index.aspx)

    Show Products
    4.1.3 添加商品(/Views/Product/Create.aspx)

    Add Product
    4.1.4 修改商品(/Views/Product/Edit.aspx)

    Update Product
    4.1.5 删除商品(/Views/Product/Index.aspx)     

    Delete Product    

    4.2,后台

       无后台。

     

    5,代码分析
     5.1,前台

      5.1.1 [只有一个示例展示,更多请下载百度文库示例案例…] 即,/Product的商品展示为例,讲解MVC和Entity运用

      5.1.1_P: MVC为什么要引入实体类,引入之后有什么好处?

      5.1.1_A: 说道好处,采用MVC架构优点是:“分离是最大的优点。”,我们知道了好处了,具体体现在哪里表现啊?

    a)有利于程序员和美工的分工合作更加清晰,真正地实现互不干扰。b)减小程序员的工作量,主要体现在在控制器和视图的数据转换,强转。

    5.1.1.1_M_Info_1,  /Models/ProductInfo.cs

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Mvc1.Models
    {
        public class ProductInfo
        {
            public int? ProductId { get; set; }
            public string ProductName { get; set; }
            public decimal? UnitPrice { get; set; }
            public string Type { get; set; }
    
        }
    }

    5.1.1.1_M_Info_2,  /Models/BaseList.cs

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Mvc1.Models
    {
        public class BaseList
        {
            /// <summary>
            /// 产品实体类集合
            /// </summary>
            public IList<ProductInfo> Prods { get; set; }
    
    
        }
    }

    5.1.1.1_M_Oper  /Models/Product.cs

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    using System.Data.OracleClient;
    namespace Mvc1.Models
    {
        public class Product
        {
            /// <summary>
            /// ylb: 1,GetAll
            /// remark: 获取所有产品,并以productId降序排列
            /// </summary>
            /// <returns></returns>
            public IList<ProductInfo> GetAll()
            {
    
                IList<ProductInfo> dals = new List<ProductInfo>();
    
                string sql = "select productId,productName,unitPrice,type from Product order by productId desc";
    
                OracleConnection conn = new DBConnection().Conn;
                OracleCommand com = conn.CreateCommand();
    
                com.CommandText = sql;
                conn.Open();
                try
                {
                    OracleDataReader sdr = com.ExecuteReader();
                    while (sdr.Read())
                    {
                        ProductInfo dal = new ProductInfo()
                        {
                            ProductId = sdr.GetInt32(0),
                            ProductName = sdr.GetString(1),
                            UnitPrice = sdr.GetDecimal(2),
                            Type = sdr.GetString(3)
                        };
    
                        dals.Add(dal);
                    }
                }
                finally
                {
                    conn.Close();
                }
                return dals;
            }
    
            /// <summary>
            /// ylb: 2,Add
            /// remark: 添加一个产品
            /// field: productName,unitPrice,type
            /// </summary>
            /// <param name="dal"></param>
            public void Add(ProductInfo dal)
            {
    
                string sql = "insert into Product(productId,productName,unitPrice,type) values(seqProduct.nextval,:productName,:unitPrice,:type)";
    
                OracleConnection conn = new DBConnection().Conn;
                OracleCommand com = conn.CreateCommand();
    
                com.Parameters.Add(new OracleParameter(":productName", dal.ProductName));
                com.Parameters.Add(new OracleParameter(":unitPrice", dal.UnitPrice));
                com.Parameters.Add(new OracleParameter(":type", dal.Type));
                com.CommandText=sql;
    
                conn.Open();
                try
                {
                    com.ExecuteNonQuery();
                }
                finally
                {
                    conn.Close();
                }
    
            }
    
    
            /// <summary>
            /// ylb: 3,GetModel
            /// remark: 获得一个实体对象,根据productId
            /// </summary>
            /// <param name="productId"></param>
            /// <returns></returns>
            public ProductInfo GetModel(int productId)
            {
                ProductInfo dal = null;
    
                string sql = "select productId,productName,unitPrice,type from Product where productId=:productId";
    
                OracleConnection conn = new DBConnection().Conn;
                OracleCommand com = conn.CreateCommand();
    
                com.Parameters.Add(new OracleParameter(":productId", productId));
                com.CommandText = sql;
                conn.Open();
                try
                {
                    OracleDataReader sdr = com.ExecuteReader();
                    while (sdr.Read())
                    {
                        dal = new ProductInfo()
                         {
                             ProductId = sdr.GetInt32(0),
                             ProductName = sdr.GetString(1),
                             UnitPrice = sdr.GetDecimal(2),
                             Type = sdr.GetString(3)
                         };
                    }
                }
                finally
                {
                    conn.Close();
                }
                return dal;
            }
    
            /// <summary>
            /// ylb: 4,Update
            /// remark: 修改一条信息 ,根据productId</summary>
            /// <param name="dal"></param>
            public void Update(ProductInfo dal)
            {
    
                string sql = "update Product set productName=:productName,unitPrice=:unitPrice,type=:type where productId=:productId";
    
                OracleConnection conn = new DBConnection().Conn;
                OracleCommand com = conn.CreateCommand();
    
                com.Parameters.Add(new OracleParameter(":productName", dal.ProductName));
                com.Parameters.Add(new OracleParameter(":unitPrice", dal.UnitPrice));
                com.Parameters.Add(new OracleParameter(":type", dal.Type));
                com.Parameters.Add(new OracleParameter(":productId", dal.ProductId));
                com.CommandText = sql;
    
                conn.Open();
                try
                {
                    com.ExecuteNonQuery();
                }
                finally
                {
                    conn.Close();
                }
    
            }
    
            /// <summary>
            /// ylb: 5,Delete
            /// remark: 删除一条信息,根据productId
            /// </summary>
            /// <param name="productId"></param>
            public void Delete(int productId)
            {
    
                string sql = "delete Product where productId=:productId";
    
                OracleConnection conn = new DBConnection().Conn;
                OracleCommand com = conn.CreateCommand();
    
                com.Parameters.Add(new OracleParameter(":productId", productId));
                com.CommandText = sql;
    
                conn.Open();
                try
                {
                    com.ExecuteNonQuery();
                }
                finally
                {
                    conn.Close();
                }
    
            }
    
        }
    }

    5.1.1.1_V  /Views/Product/Index.aspx  ylb_tip:字体加粗,字号加大的方是你要重点看的地方。

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Mvc1.Models.BaseList>" %>
    <%@Import Namespace="Mvc1.Models" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Index</title>
        <style type="text/css">
            .style1
            {
                background-color: #99CCFF;
            }
            .style2
            {
                background-color: #FFFF00;
            }
        </style>
    </head>
    <body>
        <div>
        <fieldset>
        <legend>
        <a href="/Product/Create">Add</a>
    
        </legend>
        </fieldset>
        <h2>Show Products</h2>
        <table width="500" border="1">
        <tr>
            <th class="style1">ProductId</th>
            <th class="style1">ProductName</th>
            <th class="style1">UnitPrice</th>
            <th class="style1">Type</th>
            <th class="style1">Oper</th>
        </tr>
        <%
            foreach (ProductInfo prod in Model.Prods)
            {
             %>
        <tr>
            <td class="style2"><%=prod.ProductId%></td>
            <td class="style2"><%=prod.ProductName%></td>
            <td class="style2"><%=prod.UnitPrice%></td>
            <td class="style2"><%=prod.Type%></td>
            <td class="style2">
                <a href="<%=string.Format("/Product/Delete/{0}",prod.ProductId) %>">Del</a>
                <a href="<%=string.Format("/Product/Edit/{0}",prod.ProductId) %>">Edit</a>
            </td>
        </tr>
        <%} %>
        </table>
        </div>
    </body>
    </html>

    5.1.1.1_C  /Controllers/ProductController.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    using Mvc1.Models;
    namespace Mvc1.Controllers
    {
        public class ProductController : Controller
        {
            //
            // GET: /Product/
    
            public ActionResult Index()
            {
               BaseList baseList=  new BaseList();    //创建实体类
               baseList.Prods = new Product().GetAll();    //把产品集合付给实体类
    
                return View(baseList);    //带到视图
            }
        }
    }

     5.2,后台

       无。

    6,示例|讲解案例下载

    谷歌开源代码下载:

    http://code.google.com/p/ylbtechaspnetmvc/downloads/list

    请单击“MVC+ADO.NET Entity(实体类)+Oracle”

    百度网盘  http://pan.baidu.com/s/1i49zn73

    请单击“MVC+ADO.NET Entity(实体类)+Oracle”

    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    克隆节点及添加属性节点
    元素属性的添加删除(原生js)
    清浮动方法
    css样式获取及兼容性(原生js)
    js基础---数据类型转换
    js基础---数字日期及运算
    js基础---object对象
    input询问键盘输入超时自动跳过选择默认值
    xpath定位
    selenium报错问题解决方法
  • 原文地址:https://www.cnblogs.com/ylbtech/p/2938994.html
Copyright © 2020-2023  润新知