⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productservice.cs

📁 简单手表销售管理(三层开发)源码
💻 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 + -