完整代码.net08前台以及后台(有标注),现有图片一堆,需要存到数据库,数据库弄好了,img字段,不懂怎么存图片进去,急求代码,aspx页面。谢谢!

解决方案 »

  1.   

    http://topic.csdn.net/u/20110322/21/81e36415-7c88-4976-86d1-bebff7e6cb90.html
      

  2.   

    img字段 是什么类型的啊?
      

  3.   

    byte[] buffer = imageFile.FileData.get_BinaryData() as byte[];buffer 就可以插入数据库了。数据库对应应该是Binary或者Blob或者Image类型的字段。
    下面红色部分换成你的buffer。 using (var conn = new OleDbConnection(strConn))
     {
      var comm = new OleDbCommand();
      comm.Connection = conn;
      comm.CommandText = "insert into images (FileName, Data) values (?, ?)";
      var p1 = new OleDbParameter();
      p1.DbType = DbType.String;
      p1.Value = Path.GetFileName(file);
      comm.Parameters.Add(p1);
      var p2 = new OleDbParameter();
      p2.DbType = DbType.Binary;
    p2.Value = File.ReadAllBytes(file);
      comm.Parameters.Add(p2);
        
      conn.Open();
      comm.ExecuteNonQuery();
      MessageBox.Show("Insert Successfully");
     }
    2楼 您的代码我还是看不懂 http://topic.csdn.net/u/20110322/21/81e36415-7c88-4976-86d1-bebff7e6cb90.html
      

  4.   

    Stream ms;
      byte[] picbyte;
      OpenFileDialog ofdSelectPic = new OpenFileDialog();
      if (ofdSelectPic.ShowDialog() == DialogResult.OK)
      {
      if ((ms = ofdSelectPic.OpenFile()) != null)
      {
      picbyte = new byte[ms.Length];
      ms.Position = 0;
      ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = "";  sql = "Insert into Person(Photo) values(@Image)";
      SqlCommand cmd = new SqlCommand(sql, conn);  cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
      cmd.Parameters["@Image"].Value = picbyte;  conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();  ms.Close();
      }
      } SqlConnection conn=new SqlConnection();   
     conn.ConnectionString="";   
     string strSql="";   
     SqlCommand cmd=new SqlCommand(strSql,conn);   
     conn.Open();   
     SqlDataReader reader=cmd.ExecuteReader();   
      reader.Read();   
     MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);   
     Image image=Image.FromStream(ms,true);   
     reader.Close();   
     conn.Close();   
     picturebox1.Image=image;   
    FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
    long len=mystream.Length;
    mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
    mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
    byte []box=new byte[len];   
    mystream.Read(box,0,(int)len);
    mycmd.Parameters["@image"].Value=box;
      

  5.   

    二进制图片存取
    if (this.FileUpload1.HasFile)
      {
      string strPath = FileUpload1.FileName;
      try
      {
      string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
      string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
      strPath = strPath.Substring(strPath.LastIndexOf("\\") + 1);
      FileUpload1.SaveAs.SaveAs(Server.MapPath("../UploadFile/")+fileName + extension);
      strPath = fileName + extension;
      using(SqlConnection conn =new SqlConnection(""))
      {  
      conn .Open();
      string sql = "";
      SqlCommand cmd = new SqlCommand(sql, conn );
      cmd.ExecuteNonQuery();
      conn.Close();
      }  }
      catch()
      {
      }
      }
    // 一般保存路径二进制使用
    Stream s = FileUpload1.PostedFile.InputStream;
    Byte[] buffer= new Byte[FileUpload1.PostedFile.ContentLength];
    s.Read(buffer,0,FileUpload1.PostedFile.ContentLength);
     string strsql = "insert into Tempimage(images,names)values(@ImageData,@names)";
      SqlCommand cmd= new SqlCommand(strsql, conn);
      cmd.Parameters.Add("@ImageData", SqlDbType.Image);
      cmd.Parameters.Add("@names", SqlDbType.VarChar);
      cmd.Parameters["@ImageData"].Value = buffer;
      cmd.Parameters["@names"].Value = "";
      cmd.ExecuteNonQuery();//显示
     Image1.ImageUrl="Photo.aspx?id="+Request.QueryString["id"];
    photo.aspx
    int Id=Request.QueryString["id"]==null?0:int.Parse(Request.QueryString["id"].ToString());
    using(SqlConnection conn=new SqlConnection())
    {
    conn.ConnectionString="";  
    string strSql="select * from A where Id='"+Id+"'";  
    SqlCommand cmd=new SqlCommand(strSql,conn) ;
    conn.Open();
    SqlDataReader reader=cmd.ExecuteReader();
    if(reader.Read())
    {
    Response.ContentType = "application/octet-stream";
    Response.BinaryWrite((Byte[])reader["Photo"]);
    }
    Response.End();
    }
      

  6.   

    <tr>
                                                <td align="right" style="text-align: right; width: 135px;">
                                                    图片上传:</td>
                                                    <td align="left" style="width: 228px">
                                                        <asp:FileUpload ID="FileUpload4" runat="server" />
                                                        <input id="Txt_Sell" runat="server" class="txt" maxlength="50" tabindex="4" type="text" visible="false" /></td>
                                            </tr>
    //上传文件处理
        private void UPFile(string time)
        {
            string FilePath = Server.MapPath("./") + "File";
            HttpFileCollection HFC = HttpContext.Current.Request.Files;
            for (int i = 0; i < HFC.Count; i++)
            {
                HttpPostedFile HF = HFC[i];
                if (HFC[i].ContentLength > 0 && HFC[i].FileName != "")
                {
                    //HF.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(HF.FileName));
                    string FName = HFC[i].FileName.Substring(HFC[i].FileName.LastIndexOf("."));
                    HF.SaveAs(FilePath + "//" + Convert.ToString(Convert.ToInt64(time) + i) + FName);//更改文件名
                }
            }
        }
      

  7.   

    swfuploadJQUERY 插件 多图上传 进度条Uploadify