Sunteți pe pagina 1din 7

Databases Tables Database Cart

Inventory

List of Items Ordered

Membership

Product Details

Product Reviews

Products

Shopping Cart

Transaction Details

Product.cs
public class Product { public Product() { } public string AccountName { get; set; } public int ProductCode { get; set; } public int Quantity { get; set; } public decimal TotalCost { get; set; } public string Active { get; set; } }

ProductDataAccess.cs

public class ProductDataAccess { string ProductCode; string ProductName; string ProductDescription; string ProductPrice; string ProductImage; string ProductReviews; string ProductType;

public string connectProduct = WebConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString; public void UpdateProduct() { SqlConnection myConnection = new SqlConnection(connectProduct); //Dont forget the connection string string UpdateProduct = "UPDATE ProductDetails SET Product_Code=@ProductCode, Product_Name= @ProductName, "; UpdateProduct += "Product_Description = @ProductDescription, Product_Price= @ProductPrice, "; UpdateProduct += "Product_Image = @ProductImage, Product_Reviews = @ProductReviews, Product_Type = @ProductType "; UpdateProduct += "WHERE Product_Code = @ProductCode"; SqlCommand myCommand = new SqlCommand(UpdateProduct, myConnection); myCommand.Parameters.Add(new SqlParameter("@ProductCode", SqlDbType.Int, 10)); myCommand.Parameters["@ProductCode"].Value = ProductCode; 50)); myCommand.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, myCommand.Parameters["@ProductName"].Value = ProductName; myCommand.Parameters.Add(new SqlParameter("@ProductDescription", SqlDbType.NVarChar, 50)); myCommand.Parameters["@ProductDescription"].Value = ProductDescription; myCommand.Parameters.Add(new SqlParameter("@ProductPrice", SqlDbType.Money)); myCommand.Parameters["@ProductPrice"].Value = ProductPrice; myCommand.Parameters.Add(new SqlParameter("@ProductImage", SqlDbType.VarBinary, 50)); myCommand.Parameters["@ProductImage"].Value = ProductImage;

myCommand.Parameters.Add(new SqlParameter("@ProductReviews", SqlDbType.NVarChar, 250)); myCommand.Parameters["@ProductReviews"].Value = ProductReviews; myCommand.Parameters.Add(new SqlParameter("@ProductType", SqlDbType.NVarChar, 50)); myCommand.Parameters["@ProductType"].Value = ProductType; myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); myCommand.Connection.Close(); } public void AddProduct() { SqlConnection myConnection = new SqlConnection(connectProduct); string InsertProduct = @"Insert into ProductDetails (Product_Code, Product_Name, Product_Description, Product_Price, Product_Image, Product_Reviews, Product_Type) Values (@ProductCode, @ProductName, @ProductDescription, @ProductPrice, @ProductImage, @ProductReviews, @ProductType)";

SqlCommand myCommand = new SqlCommand(InsertProduct, myConnection); myCommand.Parameters.Add(new SqlParameter("@ProductCode", SqlDbType.Int, 10)); myCommand.Parameters["@ProductCode"].Value = ProductCode; 50)); myCommand.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.NVarChar, myCommand.Parameters["@ProductName"].Value = ProductName; myCommand.Parameters.Add(new SqlParameter("@ProductDescription", SqlDbType.NVarChar, 50)); myCommand.Parameters["@ProductDescription"].Value = ProductDescription; myCommand.Parameters.Add(new SqlParameter("@ProductPrice", SqlDbType.Money)); myCommand.Parameters["@ProductPrice"].Value = ProductPrice; myCommand.Parameters.Add(new SqlParameter("@ProductImage", SqlDbType.VarBinary, 50)); myCommand.Parameters["@ProductImage"].Value = ProductImage;

myCommand.Parameters.Add(new SqlParameter("@ProductReviews", SqlDbType.NVarChar, 250)); myCommand.Parameters["@ProductReviews"].Value = ProductReviews; myCommand.Parameters.Add(new SqlParameter("@ProductType", SqlDbType.NVarChar, 50)); myCommand.Parameters["@ProductType"].Value = ProductType; myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); myCommand.Connection.Close(); } public bool AddToCart(Product product) { SqlConnection conn = new SqlConnection(connectProduct); SqlCommand cmd = new SqlCommand(); cmd.CommandText = @"SELECT * FROM Cart INNER JOIN ProductsDetails ON Cart.Product_Id = ProductsDetails.Product_Code WHERE Account_name=@Account_name AND Product_Id=@Product_Id"; cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = conn; SqlParameter ProductID = new SqlParameter("@Product_Id", System.Data.SqlDbType.Int); SqlParameter AccountName = new SqlParameter("@Account_name", System.Data.SqlDbType.VarChar); ProductID.Value = product.ProductCode; AccountName.Value = product.AccountName; cmd.Parameters.Add(ProductID); cmd.Parameters.Add(AccountName);

conn.Open(); SqlDataReader dReader = cmd.ExecuteReader(); dReader.Read(); if (dReader.HasRows == false) { dReader.Close(); cmd = new SqlCommand(); cmd.CommandText = @"INSERT INTO Cart ( Account_name, Product_Id, Quantity, TotalCost, Active) VALUES ( @Account_name, @Product_Id, @Quantity, @TotalCost, @Active)"; cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = conn; SqlParameter SqlParameter SqlParameter SqlParameter SqlParameter Account_name = new SqlParameter("@Account_name", SqlDbType.VarChar); Product_Id = new SqlParameter("@Product_Id", SqlDbType.Int); Quantity = new SqlParameter("@Quantity", SqlDbType.Int); TotalCost = new SqlParameter("@TotalCost", SqlDbType.Decimal); Active = new SqlParameter("@Active", SqlDbType.VarChar);

Account_name.Value = product.AccountName; Product_Id.Value = product.ProductCode; Quantity.Value = product.Quantity; TotalCost.Value = product.TotalCost; Active.Value = "ACTIVE"; cmd.Parameters.Add(Account_name); cmd.Parameters.Add(Product_Id); cmd.Parameters.Add(Quantity); cmd.Parameters.Add(TotalCost); cmd.Parameters.Add(Active);

if (cmd.ExecuteNonQuery() == 1) { conn.Close(); return true; } else { conn.Close(); return false; } }

else { return false; }

S-ar putea să vă placă și