如何插入和;使用linq获取图像

时间:2012-03-14 06:19:22

标签: linq

首先

id      (int)             
name (varchar)          
picture (image)  

第二

id (int)      
post (varchar)    
age (int)
  1. 我想在第一张表中添加记录。
  2. 然后我想用id。
  3. 组合这两个表
  4. 然后我想填写一个gridview,其中帖子是“manager”。
  5. (特别是我想知道如何将图像插入数据库以及如何使用网格视图显示它们。)

1 个答案:

答案 0 :(得分:0)

要在桌面上插入图片,您可以点击保存按钮

尝试这样的操作
    string fileName = FileUpload1.FileName;
    byte[] fileByte = FileUpload1.FileBytes;
    Binary binaryObj = new Binary(fileByte);
    DataClasses1DataContext context = new DataClasses1DataContext();
    //mem_images is the name of the table you dragged , use your table name here
    context.mem_images.InsertOnSubmit(new mem_image { mem_img=binaryObj  });
    context.SubmitChanges();
    Response.Write("<script>alert('Image Has Been Saved Successfully'</script>);

要从数据库表中检索图像,您需要使用通用处理程序(.ashx)文件,右键单击您的项目,然后转到添加,然后再添加新项目,最后是Generic Handler.Here是您的代码.ashx文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace CwizBankApp
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>

    public class ShowImage : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
           // context.Response.Write("Hello World");
            string id =context.Request.QueryString["Id"];


            context.Response.ContentType = "image/jpeg";
            Stream strm = ShowEmpImg(id);
            byte[] buffer = new byte[4096];
            int byteSeq = strm.Read(buffer, 0, 4096);
            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, 4096);
            }
        }
        public Stream ShowEmpImg(string id)
        {


            DataClasses1DataContext context1 = new DataClasses1DataContext();
            var r = (from a in context1.mem_images where a.image_id==id select a).First() ;
            return new MemoryStream(r.mem_img.ToArray());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

然后最终在您的节目图片的点击事件或其他任何事情

custImage1.ImageUrl = "~/ShowImage.ashx?Id="+textbox.text

希望这有助于您,如果您有进一步的问题,请回复