• <legend id='Zs22j'><style id='Zs22j'><dir id='Zs22j'><q id='Zs22j'></q></dir></style></legend>
    <i id='Zs22j'><tr id='Zs22j'><dt id='Zs22j'><q id='Zs22j'><span id='Zs22j'><b id='Zs22j'><form id='Zs22j'><ins id='Zs22j'></ins><ul id='Zs22j'></ul><sub id='Zs22j'></sub></form><legend id='Zs22j'></legend><bdo id='Zs22j'><pre id='Zs22j'><center id='Zs22j'></center></pre></bdo></b><th id='Zs22j'></th></span></q></dt></tr></i><div id='Zs22j'><tfoot id='Zs22j'></tfoot><dl id='Zs22j'><fieldset id='Zs22j'></fieldset></dl></div>

      • <bdo id='Zs22j'></bdo><ul id='Zs22j'></ul>

      1. <tfoot id='Zs22j'></tfoot>
      2. <small id='Zs22j'></small><noframes id='Zs22j'>

      3. 如何将图像插入sql server数据库?

        How to insert an image into sql server database?(如何将图像插入sql server数据库?)

          • <bdo id='ZOgq4'></bdo><ul id='ZOgq4'></ul>
              <tbody id='ZOgq4'></tbody>

            <small id='ZOgq4'></small><noframes id='ZOgq4'>

            <legend id='ZOgq4'><style id='ZOgq4'><dir id='ZOgq4'><q id='ZOgq4'></q></dir></style></legend>
            1. <i id='ZOgq4'><tr id='ZOgq4'><dt id='ZOgq4'><q id='ZOgq4'><span id='ZOgq4'><b id='ZOgq4'><form id='ZOgq4'><ins id='ZOgq4'></ins><ul id='ZOgq4'></ul><sub id='ZOgq4'></sub></form><legend id='ZOgq4'></legend><bdo id='ZOgq4'><pre id='ZOgq4'><center id='ZOgq4'></center></pre></bdo></b><th id='ZOgq4'></th></span></q></dt></tr></i><div id='ZOgq4'><tfoot id='ZOgq4'></tfoot><dl id='ZOgq4'><fieldset id='ZOgq4'></fieldset></dl></div>
                <tfoot id='ZOgq4'></tfoot>

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

                  问题描述

                  如前所述,我试图在表格中插入一个图像,其中字段的类型是 Varbinary.

                  到目前为止我做了什么:

                  我有一个包含许多字段的表单:

                  @using (Html.BeginForm("InsertProduct", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){@Html.ValidationSummary(true)<字段集><legend>产品</legend><div class="editor-label">@Html.LabelFor(model => model.PRODUCT_ID)

                  <div class="editor-field">@Html.EditorFor(model => model.PRODUCT_ID)@Html.ValidationMessageFor(model =>model.PRODUCT_ID)

                  <div class="editor-label">@Html.LabelFor(model =>model.PRODUCT_NAME)

                  <div class="editor-field">@Html.EditorFor(model =>model.PRODUCT_NAME)@Html.ValidationMessageFor(model =>model.PRODUCT_NAME)

                  <div class="editor-label">@Html.LabelFor(model => model.PRODUCT_IMAGE)

                  <div class="editor-field"><input type="file" name="PRODUCT_IMAGE" id="PRODUCT_IMAGE" style="width: 100%;"/>

                  <p><input type="submit" value="Create" class="btn btn-primary"/></p></fieldset>}

                  所有这些字段都允许我在我的控制器中构造一个 PRODUCT 对象:

                  public ActionResult InsertProduct(PRODUCT ord){已发送迁移实体 1 = 新迁移实体 1();sent.PRODUCT.Add(ord);sent.SaveChanges();列表<产品>产品 = sent.PRODUCT.ToList();返回视图(产品",产品);}

                  但是当我尝试上传图像时(通过单击创建"按钮),我有以下内容:

                  <块引用>

                  条目不是有效的 base64 字符串,因为它包含一个字符这不是基数 64

                  所以首先:这是处理图像的正确方法,其次,我想我需要对我的图像进行预处理以插入它:如何做到这一点?

                  谢谢!

                  <小时>

                  感谢收到的答案,似乎适合插入.但是对于显示,我仍然有问题(仅显示未找到的图像"图).我尝试通过两种方式做到这一点:1.并在控制器中

                  public Image LoadImage(int id){String serviceAddress = ConfigurationManager.AppSettings["WCFADDRESS"];DataServiceContext context = new DataServiceContext(new Uri(serviceAddress));PRODUCT product = context.Execute(new Uri(serviceAddress + "prod_id?prod_id=" + id)).ToList().FirstOrDefault();MemoryStream ms = new MemoryStream(product.PRODUCT_IMAGE);图像 img = Image.FromStream(ms);返回图像;}

                  还有 2.:

                  @{if (Model.product.PRODUCT_IMAGE != null){WebImage wi = new WebImage(Model.product.PRODUCT_IMAGE);wi.Resize(700, 700,true, true);wi.Write();}}

                  但是他们都没有工作.我究竟做错了什么 ?

                  解决方案

                  1) 更改您的数据库表以包含以下列:

                  1: ProductImage - varbinary(MAX)2:ImageMimeType - varchar(50)

                  2) 像这样改变你的行动方法:

                  public ActionResult InsertProduct(PRODUCT ord,HttpPostedFileBase PRODUCT_IMAGE){如果(模型状态.IsValid){MigrationEntities1 sent = new MigrationEntities1();如果(图像!= null){ord.ProductImage=新字节[PRODUCT_IMAGE.ContentLength];ord.ImageMimeType = PRODUCT_IMAGE.ContentType;PRODUCT_IMAGE.InputStream.Read(ord.ProductImage, 0,PRODUCT_IMAGE.ContentLength);}别的{//设置默认图片:图像 img = Image.FromFile(Server.MapPath(Url.Content("~/Images/Icons/nopic.png")));MemoryStream ms = new MemoryStream();img.Save(ms, ImageFormat.Png);//改成其他格式ms.Seek(0, SeekOrigin.Begin);ord.ProductImage=新字节[ms.Length];ord.ImageMimeType=图像/png";ms.Read(ord.Pic, 0, (int)ms.Length);}尝试{sent.PRODUCT.Add(ord);sent.SaveChanges();ViewBag.HasError = "0";ViewBag.DialogTitle = "插入成功";ViewBag.DialogText = "...";}抓住{ViewBag.HasError = "1";ViewBag.DialogTitle = "服务器错误!";ViewBag.DialogText = "...";}列表<产品>产品 = sent.PRODUCT.ToList();返回视图(产品",产品);}返回视图(顺序);}

                  此操作方法仅用于创建.你也需要一些编辑和索引工作.如果你在做它们时遇到问题,请告诉我将它们的代码添加到答案中.

                  更新:如何显示图像:

                  显示存储图像的一种方式如下:

                  1) 将此操作方法添加到您的控制器:

                   [AllowAnonymous]公共文件内容结果 GetProductPic(int id){产品 p = db.PRODUCTS.FirstOrDefault(n => n.ID == id);如果(p != 空){返回文件(p.ProductImage,p.ImageMimeType);}别的{返回空;}}

                  2) 在视图的 @foreach(...) 结构中添加一个 <img> 标记(或您想要的任何地方),如下所示:>

                  <img width="100" height="100" src="@Url.Action("GetProductPic", "Products", routeValues: new { id = item.ID })"/>

                  As said, i'm trying to insert an image in a table, where the type of the field is Varbinary.

                  What i've done so far :

                  I've a form with many fields:

                  @using (Html.BeginForm("InsertProduct", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
                  {
                      @Html.ValidationSummary(true)
                  
                      <fieldset>
                          <legend>PRODUCT</legend>
                  
                          <div class="editor-label">
                              @Html.LabelFor(model => model.PRODUCT_ID)
                          </div>
                          <div class="editor-field">
                              @Html.EditorFor(model => model.PRODUCT_ID)
                              @Html.ValidationMessageFor(model => model.PRODUCT_ID)
                          </div>
                  
                          <div class="editor-label">
                              @Html.LabelFor(model => model.PRODUCT_NAME)
                          </div>
                          <div class="editor-field">
                              @Html.EditorFor(model => model.PRODUCT_NAME)
                              @Html.ValidationMessageFor(model => model.PRODUCT_NAME)
                          </div>
                          <div class="editor-label">
                              @Html.LabelFor(model => model.PRODUCT_IMAGE)
                          </div>
                          <div class="editor-field">
                              <input type="file" name="PRODUCT_IMAGE" id="PRODUCT_IMAGE" style="width: 100%;" />
                          </div>
                  
                          <p>
                              <input type="submit" value="Create" class="btn btn-primary"/>
                          </p>
                      </fieldset>
                  }
                  

                  And all these fields allow me to construct a PRODUCT object in my controller :

                  public ActionResult InsertProduct(PRODUCT ord)
                      {
                          MigrationEntities1 sent = new MigrationEntities1();
                          sent.PRODUCT.Add(ord);
                          sent.SaveChanges();
                          List<PRODUCT> Products = sent.PRODUCT.ToList();
                          return View("Products", Products);
                      }
                  

                  But when i'm trying to upload the image (by clicking on Create button), i've the following :

                  entry is not a valid base64 string because it contains a character that is not base 64

                  So first of all : is it the right way to deal with images and second, I think I need to do a pre-treatemant on my image to insert it : how to o that ?

                  Thanks !


                  Edit :

                  Thanks to answers received, seems to be good for insertion. But for displaying, I still have issues (only the "not found image" piture is displayed). I've try to do it two ways : 1. <img src="LoadImage?id=@Model.product.PRODUCT_ID"/> and in the controller

                  public Image LoadImage(int id)
                      {
                          String serviceAddress = ConfigurationManager.AppSettings["WCFADDRESS"];
                          DataServiceContext context = new DataServiceContext(new Uri(serviceAddress));
                          PRODUCT product = context.Execute<PRODUCT>(new Uri(serviceAddress + "prod_id?prod_id=" + id)).ToList().FirstOrDefault();
                  
                          MemoryStream ms = new MemoryStream(product.PRODUCT_IMAGE);
                          Image img = Image.FromStream(ms);
                          return img;
                      }
                  

                  And 2. :

                  @{
                  
                      if (Model.product.PRODUCT_IMAGE != null)
                      {
                          WebImage wi = new WebImage(Model.product.PRODUCT_IMAGE);
                          wi.Resize(700, 700,true, true);
                          wi.Write();
                      }    
                  }
                  

                  But none of them are working. What am I doing wrong ?

                  解决方案

                  1) Change your database table to have these columns:

                  1: ProductImage - varbinary(MAX)
                  2: ImageMimeType - varchar(50)
                  

                  2) Change your action method like this:

                  public ActionResult InsertProduct(PRODUCT ord, 
                      HttpPostedFileBase PRODUCT_IMAGE)
                  {        
                      if (ModelState.IsValid)        
                          {
                              MigrationEntities1 sent = new MigrationEntities1();
                              if (image != null)
                              {
                                  ord.ProductImage= new byte[PRODUCT_IMAGE.ContentLength];
                                  ord.ImageMimeType = PRODUCT_IMAGE.ContentType;
                                  PRODUCT_IMAGE.InputStream.Read(ord.ProductImage, 0,
                                      PRODUCT_IMAGE.ContentLength);
                              }
                  
                              else
                              {
                                  // Set the default image:
                                  Image img = Image.FromFile(
                                      Server.MapPath(Url.Content("~/Images/Icons/nopic.png")));
                                  MemoryStream ms = new MemoryStream();
                                  img.Save(ms, ImageFormat.Png); // change to other format
                                  ms.Seek(0, SeekOrigin.Begin);
                                  ord.ProductImage= new byte[ms.Length];
                                  ord.ImageMimeType= "image/png";
                                  ms.Read(ord.Pic, 0, (int)ms.Length);
                              }
                  
                              try
                              {
                                  sent.PRODUCT.Add(ord);
                                  sent.SaveChanges();
                  
                                  ViewBag.HasError = "0";
                                  ViewBag.DialogTitle = "Insert successful";
                                  ViewBag.DialogText = "...";
                              }
                              catch
                              {
                                  ViewBag.HasError = "1";
                                  ViewBag.DialogTitle = "Server Error!";
                                  ViewBag.DialogText = "...";
                              }
                  
                              List<PRODUCT> Products = sent.PRODUCT.ToList();
                              return View("Products", Products);
                          }
                  
                          return View(ord);
                      }
                  

                  This action method is just for create. you need some works for edit and index too. If you have problem to doing them, tell me to add codes of them to the answer.

                  Update: How to show images:

                  One way to show stored images is as the following:

                  1) Add this action method to your controller:

                      [AllowAnonymous]
                      public FileContentResult GetProductPic(int id)
                      {
                          PRODUCT p = db.PRODUCTS.FirstOrDefault(n => n.ID == id);
                          if (p != null)
                          {
                              return File(p.ProductImage, p.ImageMimeType);
                          }
                  
                          else
                          {
                              return null;
                          }
                      }
                  

                  2) Add a <img> tag in the @foreach(...) structure of your view (or wherever you want) like this:

                  <img width="100" height="100" src="@Url.Action("GetProductPic", "Products", routeValues: new { id = item.ID })" />
                  

                  这篇关于如何将图像插入sql server数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

                  1. <small id='wxsu8'></small><noframes id='wxsu8'>

                    <i id='wxsu8'><tr id='wxsu8'><dt id='wxsu8'><q id='wxsu8'><span id='wxsu8'><b id='wxsu8'><form id='wxsu8'><ins id='wxsu8'></ins><ul id='wxsu8'></ul><sub id='wxsu8'></sub></form><legend id='wxsu8'></legend><bdo id='wxsu8'><pre id='wxsu8'><center id='wxsu8'></center></pre></bdo></b><th id='wxsu8'></th></span></q></dt></tr></i><div id='wxsu8'><tfoot id='wxsu8'></tfoot><dl id='wxsu8'><fieldset id='wxsu8'></fieldset></dl></div>
                    • <bdo id='wxsu8'></bdo><ul id='wxsu8'></ul>
                    <legend id='wxsu8'><style id='wxsu8'><dir id='wxsu8'><q id='wxsu8'></q></dir></style></legend>

                      <tfoot id='wxsu8'></tfoot>