Advertisement
Guest User

Untitled

a guest
Jan 1st, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. using Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Web;
  12. using System.Web.Mvc;
  13.  
  14. namespace OnlineStore.Controllers
  15. {
  16. public class IO
  17. {
  18. public static byte[] ImageInsert(Image pf)
  19. {
  20.  
  21. MemoryStream ms = new MemoryStream();
  22. pf.Save(ms, ImageFormat.Jpeg);
  23. byte[] bt = ms.ToArray();
  24. return bt;
  25. }
  26.  
  27. public static Image ImageRead(byte[] image) {
  28.  
  29. System.Drawing.Image btm = System.Drawing.Image.FromStream(new MemoryStream(image));
  30.  
  31. return btm;
  32. }
  33.  
  34. public static void Insert(Product pr)
  35. {
  36.  
  37. using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Database"]))
  38. {
  39. connection.Open();
  40.  
  41. string s = "Insert into Table Product(Id,Name,Price,Description,Image) values (@GUID,@Name,@Price,@Description,@Category,@Image)";
  42. using (SqlCommand cmd = new SqlCommand(s, connection))
  43. {
  44. cmd.Parameters.Add("@GUID", SqlDbType.UniqueIdentifier).Value = new Guid();
  45. cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = pr.Name;
  46. cmd.Parameters.Add("@Price", SqlDbType.Int).Value = pr.Price;
  47. cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = pr.Description;
  48. cmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = pr.Description;
  49. cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = IO.ImageInsert(pr.Picture);
  50.  
  51. cmd.BeginExecuteNonQuery();
  52. }
  53. }
  54. }
  55.  
  56. public static List<Product> Read() {
  57.  
  58. List<Product> lst;
  59.  
  60. using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Database"]))
  61. {
  62. string s = "select * from dbo.Products";
  63. lst = new List<Product>();
  64.  
  65. using (SqlCommand sqm = new SqlCommand(s, connection))
  66. {
  67. using (SqlDataReader sdr = sqm.ExecuteReader())
  68. {
  69. if (sdr.Read())
  70. {
  71. lst.Add(new Product()
  72. {
  73.  
  74. Name = (string)sdr.GetValue(1),
  75. Price = (int)sdr.GetValue(2),
  76. Description = (string)sdr.GetValue(3),
  77. Picture = ImageRead((byte[])sdr.GetValue(4)),
  78. Category = (string)sdr.GetValue(5)
  79.  
  80. });
  81. }
  82. }
  83. }
  84. }
  85. return lst;
  86. }
  87.  
  88. }
  89.  
  90.  
  91. public class HomeController : Controller
  92. {
  93. [HttpPost]
  94. public ActionResult Form(Product p)
  95. {
  96. IO.Insert(p);
  97. return RedirectToAction("Index");
  98. }
  99.  
  100. public ActionResult Index()
  101. {
  102. return View();
  103. }
  104.  
  105. public ActionResult Products()
  106. {
  107. return View();
  108. }
  109.  
  110. public ActionResult Product()
  111. {
  112. return View();
  113. }
  114.  
  115. public ActionResult Cart()
  116. {
  117. return View();
  118. }
  119.  
  120. public ActionResult About()
  121. {
  122.  
  123. return View();
  124. }
  125.  
  126. public ActionResult Contact()
  127. {
  128.  
  129. return View();
  130. }
  131.  
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement