我在数据库中写了三了字段,产品编号,产品名称,产品图片。我现在从后台中已经把数据插入到数据库中了,怎么样在前台显示这个产品的图片和信息!源码看看!谢谢!!!

解决方案 »

  1.   

    http://topic.csdn.net/t/20050126/17/3755953.html
    去看看
      

  2.   

    这个关于图片在数据库中存放,有两种方式,一种是存放路径,这个我想是大部分人都在使用的,还有的就是把图片变成二进制存放到数据库表字段中,这样的话,可以不需要额外的空间来存放图片,这两种方式的网上都有很详细的代码,楼主可以去cnblogs上找找
      

  3.   

    一般读取图片的那部分单独用.ashx做. 然后就是绑定了. 如GridView的TemplateField里有个Image控件的话, <asp:Image ImageUrl='xxx.ashx?id=<%# Eval("ID") %>' ... />
      

  4.   

      //绑定GridView
        public void bind()
        {
            string sqlstr = "select  [ImageName],[ImageID] from [Image]";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "Admin");
            GridView1.DataSource = myds;
            GridView1.DataBind();
        }
        protected void Bt_Cacel_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
        }
        //保存图片
        protected void Bt_Save_Click(object sender, EventArgs e)
        {        
            Stream imgStream=FU_image.PostedFile.InputStream;
            int imgLen=FU_image.PostedFile.ContentLength;
            string imgName=this.T_image.Text;
            byte[] imgBinaryData=new byte[imgLen];
            int n=imgStream.Read(imgBinaryData,0,imgLen);
        
         SqlConnection connection = new SqlConnection("Data Source=(local);Database=wxd;Uid=sa;Pwd=sa");
         SqlCommand command = new SqlCommand("insert into Image (ImageName,Image) values ( @img_name, @img_data)", connection);
     
         SqlParameter param0 = new SqlParameter("@img_name", SqlDbType.VarChar, 50);
         param0.Value = imgName;
         command.Parameters.Add(param0);
     
         SqlParameter param1 = new SqlParameter("@img_data", SqlDbType.Image);
         param1.Value = imgBinaryData;
         command.Parameters.Add(param1);
     
         connection.Open();
         int numRowsAffected = command.ExecuteNonQuery();
         connection.Close();
         this.Panel2.Visible = false;
         this.T_image.Text = "";
         bind();
        }
        //显示保存
      

  5.   

    protected void btntj_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string fileContentType = FileUpload1.PostedFile.ContentType;//获取客户端用户发送的文件类型
                if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")//判断图片的类型
                {
                    string name = FileUpload1.PostedFile.FileName;                  // 客户端文件路径                FileInfo file = new FileInfo(name);
                    string fileName = file.Name;                                    // 文件名称
                    string fileName_s = "x_" + file.Name;                           // 缩略图文件名称
                    string fileName_sy = "text_" + file.Name;                         // 水印图文件名称(文字)
                    string fileName_syp = "water_" + file.Name;                       // 水印图文件名称(图片)
                    string webFilePath = Server.MapPath(fileName);        // 服务器端文件路径
                    string webFilePath_s = Server.MapPath(fileName_s);  // 服务器端缩略图路径
                    string webFilePath_sy = Server.MapPath(fileName_sy); // 服务器端带水印图路径(文字)
                    string webFilePath_syp = Server.MapPath(fileName_syp); // 服务器端带水印图路径(图片)                if (!File.Exists(webFilePath))//判断服务器端文件是否存在
                    {
                        try
                        {
                            //将信息保存在数据库中
                            byte[] fileData = FileUpload1.FileBytes;
                            SqlConnection con = new SqlConnection(@"server=.;uid=sa;pwd=sa;database=Wish");
                            SqlCommand com = new SqlCommand("InsertChanPin", con);
                            com.CommandType = CommandType.StoredProcedure;
                            com.Parameters.Add("@cpName", SqlDbType.VarChar, 50).Value = txtName.Text;
                            com.Parameters.Add("@cpId", SqlDbType.VarChar,50).Value = txtId.Text;
                            com.Parameters.Add("@cpImages", SqlDbType.Image).Value =fileData;
                            con.Open();
                            com.ExecuteNonQuery();
                            con.Close();
                            FileUpload1.SaveAs(webFilePath);                                // 使用 SaveAs 方法保存文件
                            Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
                        }
                        catch (Exception ex)
                        {
                            Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
                        }
                    }
                    else
                    {
                        Label1.Text = "提示:文件已经存在,请重命名后上传";
                    }
                }
                else
                {
                    Label1.Text = "提示:文件类型不符";
                }
            }
        }这个是我的后台代码!!!!!!
      

  6.   

    <Columns>
                                  <asp:BoundField DataField="ImageID" HeaderText="编号" />
                                    <asp:BoundField DataField="ImageName" HeaderText="用户名称" />
                                    <asp:TemplateField HeaderText="头像">                               
                                        <ItemTemplate>
                                            <asp:Image ID="Image1" Height="80" Width="100"  runat="server" ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("ImageID")  %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
      

  7.   

    <%@ WebHandler Language="C#" Class="ImageHandler" %>using System;
    using System.Web;
    using System.Data.SqlClient;public class ImageHandler : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) 
        {
             string imageid = context.Request.QueryString["ImID"];
             SqlConnection connection = new SqlConnection("Data Source=(local);Database=wxd;Uid=sa;Pwd=sa");
             connection.Open();
             SqlCommand command = new SqlCommand("select [Image] from [Image] where ImageID=" + imageid, connection);
             SqlDataReader dr = command.ExecuteReader();
             dr.Read();
             context.Response.BinaryWrite((Byte[])dr[0]);
             connection.Close();
             context.Response.End();    }
     
        public bool IsReusable
        {
            get {
                return false;
            }
        }}
      

  8.   

    string imageid = context.Request.QueryString["ImID"];是什么意识,,
    我主要做的一个网站是从后台添加产品,然后在产品的页面里面将后台传过去的读出来,能按我的表的设计详细点吗?我是新手,谢谢!!!!