using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class Product : IDataErrorInfo,INotifyPropertyChanged
{
private int _ProductID = 0;
public int ProductID
{
get { return _ProductID; }
set
{
_ProductID = value;
RaisePropertyChanged("ProductID");
}
}
private int _CategoryID = 0;
public int CategoryID
{
get { return _CategoryID; }
set
{
_CategoryID = value;
RaisePropertyChanged("CategoryID");
}
}
private string _ModelNumber = "";
public string ModelNumber
{
get { return _ModelNumber; }
set
{
_ModelNumber = value;
RaisePropertyChanged("ModelNumber");
}
}
private string _ModelName = "";
public string ModelName
{
get { return _ModelName; }
set
{
_ModelName = value;
RaisePropertyChanged("ModelName");
}
}
private string _ProductImage = "";
public string ProductImage
{
get { return _ProductImage; }
set
{
_ProductImage = value;
RaisePropertyChanged("ProductImage");
}
}
private decimal _UnitCost = 0;
public decimal UnitCost
{
get { return _UnitCost; }
set
{
_UnitCost = value;
RaisePropertyChanged("UnitCost");
}
}
private string _Description = "";
public string Description
{
get { return _Description; }
set
{
_Description = value;
RaisePropertyChanged("Description");
}
}
public string Error
{
get{return null;}
}
public string this[string columnName]
{
get
{
bool valid = true;
if (columnName == "UnitCost")
{
if (UnitCost < 0)
{
valid = false;
}
}
if (!valid)
{
return "UnitCost can not be negative";
}
else
{
return null;
}
}
}
public Product(int CategoryID = 0, string ModelNumber = "",
string ModelName = "", string ProductImage = "", decimal UnitCost = 0, string Description = "")
{
this.CategoryID = CategoryID;
this.ModelNumber = ModelNumber;
this.ModelName = ModelName;
this.ProductImage = ProductImage;
this.UnitCost = UnitCost;
this.Description = Description;
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
StoreDB.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class StoreDB
{
public static string connString = Properties.Settings.Default.ConnectionString;
public static ObservableCollection<Product> GetProducts()
{
ObservableCollection<Product> products = new ObservableCollection<Product>();
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("GetProducts", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
var p = new Product()
{
ProductID = (int)reader[0],
CategoryID = (int)reader[1],
ModelNumber = reader[2].ToString(),
ModelName = reader[3].ToString(),
ProductImage = reader[4].ToString(),
UnitCost = (decimal)reader[5],
Description = reader[6].ToString()
};
products.Add(p);
}
return products;
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public static Product GetProductByID(int id)
{
Product p = null;
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("GetProductByID", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProductID", id);
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
p = new Product()
{
CategoryID = (int)reader[1],
ModelNumber = reader[2].ToString(),
ModelName = reader[3].ToString(),
ProductImage = reader[4].ToString(),
UnitCost = (decimal)reader[5],
Description = reader[6].ToString()
};
}
return p;
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public static void UpdateProductByID(int ProductID, Product p)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("UpdateProductByID", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProductID", ProductID);
cmd.Parameters.AddWithValue("@CategoryID", p.CategoryID);
cmd.Parameters.AddWithValue("@ModelNumber", p.ModelNumber);
cmd.Parameters.AddWithValue("@ModelName", p.ModelName);
cmd.Parameters.AddWithValue("@ProductImage", p.ProductImage);
cmd.Parameters.AddWithValue("@UnitCost", p.UnitCost);
cmd.Parameters.AddWithValue("@Description", p.Description);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public static void InsertProduct(Product p)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("InsertProduct", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", p.CategoryID);
cmd.Parameters.AddWithValue("@ModelNumber", p.ModelNumber);
cmd.Parameters.AddWithValue("@ModelName", p.ModelName);
cmd.Parameters.AddWithValue("@ProductImage", p.ProductImage);
cmd.Parameters.AddWithValue("@UnitCost", p.UnitCost);
cmd.Parameters.AddWithValue("@Description", p.Description);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public static void DeleteProductByID(int id)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("DeleteProductByID", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProductID", id);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
}
}
ViewModel.cs
using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp4
{
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<Product> _Products = null;
public ObservableCollection<Product> Products
{
get { return _Products; }
set
{
_Products = value;
RaisePropertyChanged("Products");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
前台
using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp4
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public ObservableCollection<Product> products = null;
public ViewModel vm = new ViewModel();
private void GetProducts_Click(object sender, RoutedEventArgs e)
{
products = vm.Products;
products = StoreDB.GetProducts();
lstProducts.ItemsSource = products;
lstProducts.DisplayMemberPath = "ModelName";
}
private void InsertProduct_Click(object sender, RoutedEventArgs e)
{
int categoryID = Convert.ToInt32(txtCategoryID.Text);
decimal unitCost = Convert.ToDecimal(txtUnitCost.Text);
Product p = new Product() { CategoryID = categoryID, ModelNumber = txtModelNumber.Text, ModelName = txtModelName.Text, ProductImage = txtProductImage.Text, UnitCost = unitCost, Description = txtDescription.Text };
StoreDB.InsertProduct(p);
products.Add(p);
}
private void DeleteProduct_Click(object sender, RoutedEventArgs e)
{
Product p = (Product)lstProducts.SelectedItem;
products.Remove(p);
StoreDB.DeleteProductByID(p.ProductID);
}
}
}
后台
using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp4
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public ObservableCollection<Product> products = null;
public ViewModel vm = new ViewModel();
private void GetProducts_Click(object sender, RoutedEventArgs e)
{
products = vm.Products;
products = StoreDB.GetProducts();
lstProducts.ItemsSource = products;
lstProducts.DisplayMemberPath = "ModelName";
}
private void InsertProduct_Click(object sender, RoutedEventArgs e)
{
int categoryID = Convert.ToInt32(txtCategoryID.Text);
decimal unitCost = Convert.ToDecimal(txtUnitCost.Text);
Product p = new Product() { CategoryID = categoryID, ModelNumber = txtModelNumber.Text, ModelName = txtModelName.Text, ProductImage = txtProductImage.Text, UnitCost = unitCost, Description = txtDescription.Text };
StoreDB.InsertProduct(p);
products.Add(p);
}
private void DeleteProduct_Click(object sender, RoutedEventArgs e)
{
Product p = (Product)lstProducts.SelectedItem;
products.Remove(p);
StoreDB.DeleteProductByID(p.ProductID);
}
}
}