?? productservice.cs
字號:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using MyWatchShop.Models;
namespace MyWatchShop.DAL
{
public class ProductService
{
public static IList<Product> GetAllProducts()
{
List<Product> list = new List<Product>();
string sql = "select * from Product";
DataTable dt = DbHelper.GetDataTable(sql);
foreach (DataRow dr in dt.Rows)
{
Product product = new Product();
product.Id = Convert.ToInt32(dr["Id"]);
product.Name = dr["Name"].ToString();
product.Oldprice = Convert.ToDecimal(dr["Oldprice"]);
product.Newprice = Convert.ToDecimal(dr["Newprice"]);
product.Smallpicture = dr["Smallpicture"].ToString();
list.Add(product);
}
return list;
}
public static bool UpdateProduct(int id, string name, decimal oldprice, decimal newprice, string smallpicture)
{
string sql = string.Format("update Product set Name='{0}',OldPrice={1},NewPrice={2},SmallPicture='{3}' where Id={4}",name,oldprice,newprice,smallpicture,id);
int result = DbHelper.ExeCommand(sql);
if (result == 1)
{
return true;
}
return false;
}
public static bool AddProduct(string name, decimal oldprice, decimal newprice, string smallpicture)
{
Product product = new Product();
product.Name = name;
product.Oldprice = oldprice;
product.Newprice = newprice;
product.Smallpicture = smallpicture;
return AddProduct(product);
}
public static bool AddProduct(Product product)
{
string sql = "insert Product(Name,OldPrice,NewPrice,SmallPicture) values (@Name,@OldPrice,@NewPrice,@SmallPicture)";
int result = DbHelper.ExeCommand(sql,new SqlParameter[]
{
new SqlParameter("@Name",product.Name),
new SqlParameter("@OldPrice",product.Oldprice),
new SqlParameter("@NewPrice",product.Newprice),
new SqlParameter("@SmallPicture",product.Smallpicture)
});
if (result==1)
{
return true;
}
return false;
}
public static Product GetProductById(int id)
{
string sql = "select * from Product where Id=@id";
DataTable dt = DbHelper.GetDataTable(sql, new SqlParameter("@id", id));
Product product = null;
if (dt.Rows.Count > 0)
{
DataRow dr = dt.Rows[0];
product = new Product();
product.Id = Convert.ToInt32(dr["Id"]);
product.Name = dr["Name"].ToString();
product.Oldprice = Convert.ToDecimal(dr["Oldprice"]);
product.Newprice = Convert.ToDecimal(dr["Newprice"]);
product.Smallpicture = dr["Smallpicture"].ToString();
}
return product;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -