将数据从action方法发送到sql server数据库

时间:2014-05-26 10:21:38

标签: sql-server asp.net-mvc-4

我有这个动作方法

 [HttpPost]
   public void Test(HttpPostedFileBase file)
   {
    byte[] image = new byte[file.ContentLength];
    file.InputStream.Read(image, 0, image.Length);
   }

我想将此操作方法中的字节数组发送到表格列中的sql server数据库....我该怎么办?

1 个答案:

答案 0 :(得分:0)

public class MyImage
{
    public byte[] Image { get; set; }
}


   [HttpPost]
   public void Test(HttpPostedFileBase file)
   {
    byte[] image = new byte[file.ContentLength];
    file.InputStream.Read(image, 0, image.Length);
    var i = new MyImage { Image = image };
    dbcontext.MyEntity.Add(i);
    dbcontext.SaveChanges(); 
   }

只需将字节数组保存在实体属性中,它就可以正常工作。