ASP.NET MVC:解析 MVC+ADO.NET Entity(实体类)+Oracle
2013-03-01 16:40
357 查看
1,功能描述 |
2,技术与环境 |
操作系统: | windows | 开发语言: | C# |
开发框架: | ASP.NET MVC 2.0 | 数据库: | Oracle |
开发软件: | Microsoft Visual Studio 2010 | ||
开发技术 | ASP.NET MVC +ADO.NET Entity | ||
项目组长: | yuanbo | 成员: | null |
3,数据库设计 |
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”
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
相关文章推荐
- ASP.NET MVC:解析 MVC+ADO.NET Entity(实体类)
- 热门的技术教程!ASP.NET MVC, JQuery, ASP.NET AJAX and ADO.NET Entity Framework
- 实战asp.net MVC+ADO.NET EntityFramework
- 实战asp.net MVC+ADO.NET EntityFramework
- 热门的技术教程!ASP.NET MVC, JQuery, ASP.NET AJAX and ADO.NET Entity Framework
- ASP.Net中Ado.Net Entity Framework实际项目应用释疑
- 解析ASP.NET WebForm和Mvc开发的区别
- ASP.NET MVC+EasyUI+Entity FrameWork 整合开发
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之目录导航
- 解析ASP.NET MVC开发之查询数据实例
- [原创]ASP.NET MVC多域名多站点解析问题
- ASP.NET MVC+Entity Framework 4.1访问数据库
- [转]Using the Repository Pattern with ASP.NET MVC and Entity Framework
- ASP.NET MVC中解析淘宝网页出现乱码问题的解决方法
- 使用Northwind和Entity框架的ASP.NET MVC实例发布
- asp.net mvc 碰到 XML 解析错误:找不到根元素 位置
- 白话ASP.NET MVC之三:Controller是如何解析出来的
- 白话ASP.NET MVC之三:Controller是如何解析出来的
- ASP.NET MVC 关于MasterPage中<script>标签的文件路径自动解析