如何在MVC中将图像插入数据库 [英] How to insert image into database in MVC

查看:99
本文介绍了如何在MVC中将图像插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......

我是MVC的新手,我想将图像插入数据库,但确实找到了一个可靠的解决方案,我怎么做...我搜索了很多但是我没有找到任何解决我的问题的答案..我不明白我必须从哪里开始,请提前帮助退出它感谢



我尝试过:



这是我的产品型号

Hi Everyone...
I am new to MVC and i want to insert image into database but does find a reliable solution that how i can do it.. i have searched a lot but i did not found any answer that solve my issue.. i do not understand where i have to start, kindly help to get out of it thankx in advance

What I have tried:

Here is my "Product" Model

public class Products
  {
      [Required]
      public int P_ID { get; set; }
      [Required]
      public string P_Name { get; set; }
      [Required]
      public string P_Image { get; set; }
      [Required]
      public int P_Price { get; set; }
      [Required]
      public int P_CatedoryID { get; set; }
  }





我的主管人员:



My Controller:

[HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult CreateProductsByAjaxCall(Products product)
     {
         if (ModelState.IsValid)
         {
             db.CreateProducts(product);
         }
         return RedirectToAction("Index");
     }





我的数据库功能



My DB Function

public void CreateProducts(Products product)
 {
     string query = "Insert into Products (P_Name,P_Image,P_Price,P_CatagoryID) Values (@name,@image,@price,@category)";
     SqlConnection con = new SqlConnection(con_string);
     con.Open();
     SqlCommand cmd = new SqlCommand(query,con);
     cmd.Parameters.AddWithValue("@name",product.P_Name);
     cmd.Parameters.AddWithValue("@image", product.P_Image);
     cmd.Parameters.AddWithValue("@price",product.P_Price);
     cmd.Parameters.AddWithValue("@category", product.P_CatedoryID);
     cmd.ExecuteNonQuery();
     con.Close();
 }

推荐答案

您将引用此链接... 点击此处
you will refer this link... Click here


您可以参考以下文章上传/下载数据库MVC中的图像 [ ^ ]
You can refer following article Upload/Download Image in/from Database MVC[^]


这篇关于如何在MVC中将图像插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆