Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Model;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace OnlineStore.Controllers
- {
- public class IO
- {
- public static byte[] ImageInsert(Image pf)
- {
- MemoryStream ms = new MemoryStream();
- pf.Save(ms, ImageFormat.Jpeg);
- byte[] bt = ms.ToArray();
- return bt;
- }
- public static Image ImageRead(byte[] image) {
- System.Drawing.Image btm = System.Drawing.Image.FromStream(new MemoryStream(image));
- return btm;
- }
- public static void Insert(Product pr)
- {
- using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Database"]))
- {
- connection.Open();
- string s = "Insert into Table Product(Id,Name,Price,Description,Image) values (@GUID,@Name,@Price,@Description,@Category,@Image)";
- using (SqlCommand cmd = new SqlCommand(s, connection))
- {
- cmd.Parameters.Add("@GUID", SqlDbType.UniqueIdentifier).Value = new Guid();
- cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = pr.Name;
- cmd.Parameters.Add("@Price", SqlDbType.Int).Value = pr.Price;
- cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = pr.Description;
- cmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = pr.Description;
- cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = IO.ImageInsert(pr.Picture);
- cmd.BeginExecuteNonQuery();
- }
- }
- }
- public static List<Product> Read() {
- List<Product> lst;
- using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Database"]))
- {
- string s = "select * from dbo.Products";
- lst = new List<Product>();
- using (SqlCommand sqm = new SqlCommand(s, connection))
- {
- using (SqlDataReader sdr = sqm.ExecuteReader())
- {
- if (sdr.Read())
- {
- lst.Add(new Product()
- {
- Name = (string)sdr.GetValue(1),
- Price = (int)sdr.GetValue(2),
- Description = (string)sdr.GetValue(3),
- Picture = ImageRead((byte[])sdr.GetValue(4)),
- Category = (string)sdr.GetValue(5)
- });
- }
- }
- }
- }
- return lst;
- }
- }
- public class HomeController : Controller
- {
- [HttpPost]
- public ActionResult Form(Product p)
- {
- IO.Insert(p);
- return RedirectToAction("Index");
- }
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult Products()
- {
- return View();
- }
- public ActionResult Product()
- {
- return View();
- }
- public ActionResult Cart()
- {
- return View();
- }
- public ActionResult About()
- {
- return View();
- }
- public ActionResult Contact()
- {
- return View();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement