您的位置:首页 > 数据库 > Oracle

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

2013-03-01 16:40 357 查看
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,数据库设计
数据关系图:

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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: