打印本文 打印本文  关闭窗口 关闭窗口
开发BtoC电子商务系统(ASP.NET)
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 13:31:20
DataRow GetProduct(int productID)
{
SqlConnection sqlConnect= new SqlConnection
("server=(local);database=CornfieldGrocer;Trusted_Connection=yes");
SqlDataAdapter sqlAdapter1 = new SqlDataAdapter
("Select * from Products where productID=" + productID, sqlConnect);
DataSet product = new DataSet();
sqlAdapter1.Fill(product, "product");
return product.Tables[0].Rows[0];
}
public DataTable GetProductCalories(int productID)
{
SqlConnection sqlConnect = new SqlConnection
("server=(local);database=CornfieldGrocer;Trusted_Connection=yes");
SqlDataAdapter sqlAdapter1 = new SqlDataAdapter
("Select * from Details where productID="+productID,sqlConnect);
DataSet details = new DataSet();
sqlAdapter1.Fill(details, "details");
return details.Tables[0];
}
}

public class OrderItem
{
public int productID;
public int quantity;
public String name;
public double price;
public OrderItem(int productID, String name, double price, int quantity)
{
this.productID = productID;
this.quantity = quantity;
this.name = name;
this.price = price;
}
public int ProductID
{
get { return ProductID; }
}
public int Quantity
{
get { return quantity; }
set { quantity=value; }
}
public String Name
{
get { return name; }
}
public double Price
{
get { return price; }
}
public double Total
{
get { return quantity * price; }
}
}

public class OrderList
{
private Hashtable orders = new Hashtable();
private double taxRate = 0.08;
public double SubTotal
{
get
{
if (orders.Count == 0)
return 0.0;
double subTotal = 0;
IEnumerator items = orders.Values.GetEnumerator();
while(items.MoveNext())
{
subTotal += ((OrderItem) items.Current).Price *
((OrderItem) items.Current).Quantity;
}
return subTotal;
}
}
public double TaxRate
{
get { return taxRate; }
set { taxRate = value; }
}
public double Tax
{
get { return SubTotal * taxRate; }
}
public double Total
{
get { return SubTotal * (1 + taxRate); }
}
public ICollection Values {
get {
return orders.Values;
}
}
public OrderItem this[String name] {
get {
return (OrderItem) orders[name];
}
}
public void Add(OrderItem value)
{
if (orders[value.Name] == null) {
orders.Add(value.Name, value);
}
else
{
OrderItem oI = (OrderItem)orders[value.Name];
oI.Quantity = oI.Quantity + 1;
}
}
public void ClearCart() {
orders.Clear();
}
}
}

需要说明的是我们将三个文件CornfieldGrocer.cs,Default.aspx.cs,Global.asax.cs用编译命令“csc /t:library /out:CornfieldGrocer.dll cornfieldgrocer.cs default.aspx.cs global.asax.cs”将它们全部封装在CornfieldGrocer命名空间里,虽然这并不是必须的。上面的编译器输出CornfieldGrocer.dll文件,我们配置该网上零售站点时只需将该文件拷贝到站点根目录中的Bin目录下即可。
到此为止,我们已经完整的向大家展示了利用ASP.NET Web表单建立一个小型的网上交易系统的编码,配置等工作。当然作为演示案例,它还没有真正系统的完善的性能,安全,界面等各个方面的优化考虑和设计。但它向我们展示的ASP.NET Web表单模型却非常典型且底层,大家不防在此基础上通过不断的修改和扩充来开发适合自己的交易系统。比如对于Default.aspx文件中AutoEventWireup="false"如果设置为“true”或去掉这个语句,在运行页面时会出现什么情况?通过这些练习便会不断的加深我们对ASP.NET底层的认识,最后达到游刃有余的把握。实际上技术的学习,尤其是编程,除了一定的兴趣和悟性外,大量代码实例的锻炼也是很有必要的,这本身就是笔者成长的一个过程,也是本文中笔者竭力要给大家展示的。

上一页  [1] [2] 



打印本文 打印本文  关闭窗口 关闭窗口