如题

解决方案 »

  1.   

    <!--
    引用地址:http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=185154
    -->求救!asp.net+c#上传图片到access数据库及显示图片的代码?
    主要解答者:redbb提交人:redbb
    感谢:sjc0、redbb、xhzuo
    审核者:webdiyer社区对应贴子:查看
         A : 不要VB.net和带SQL  Server存储过程的方法,用不上哦!  
    ---------------------------------------------------------------  
     
    我用了存储过程不过你改改  
                           Stream  stream  =  insertimg.PostedFile.InputStream;  
                           byte[]  buffer  =  new  byte[insertimg.PostedFile.ContentLength];  
                           int  aa  =  stream.Read(buffer,  0,  (int)stream.Length);      
                   string  filename  =  System.IO.Path.GetFileNameWithoutExtension(insertimg.PostedFile.FileName.ToString());  
                           string  expandname  =  Path.GetExtension(insertimg.PostedFile.FileName.ToString());  
                           int  size  =  (int)stream.Length;  
                           stream.Read(buffer,  0,  size);  
    你把buffer传入你的image字段就可以了!  
    ---------------------------------------------------------------  
     
    store  it  :    
     
     
     Stream  imgdatastream  =  File1.PostedFile.InputStream;    
           
       int  imgdatalen  =  File1.PostedFile.ContentLength;    
           
       string  imgtype  =  File1.PostedFile.ContentType;    
           
       string  imgtitle  =  TextBox1.Text;    
           
       byte[]  imgdata  =  new  byte[imgdatalen];    
           
       int  n  =  imgdatastream.Read(imgdata,0,imgdatalen);    
           
       string  connstr=    
           
       ((NameValueCollection)Context.GetConfig    
           
       ("appSettings"))["connstr"];    
           
       SqlConnection  connection  =  new  SqlConnection(connstr);    
           
       SqlCommand  command  =  new  SqlCommand    
           
       ("INSERT  INTO  ImageStore(imgtitle,imgtype,imgdata)    
           
       VALUES  (  @imgtitle,  @imgtype,@imgdata  )",  connection  );    
           
           
           
       SqlParameter  paramTitle  =  new  SqlParameter    
           
       ("@imgtitle",  SqlDbType.VarChar,50  );    
           
       paramTitle.Value  =  imgtitle;    
           
       command.Parameters.Add(  paramTitle);    
           
           
           
       SqlParameter  paramData  =  new  SqlParameter    
           
       (  "@imgdata",  SqlDbType.Image  );    
           
       paramData.Value  =  imgdata;    
           
       command.Parameters.Add(  paramData  );    
           
           
           
       SqlParameter  paramType  =  new  SqlParameter    
           
       (  "@imgtype",  SqlDbType.VarChar,50  );    
           
       paramType.Value  =  imgtype;    
           
       command.Parameters.Add(  paramType  );    
           
           
           
       connection.Open();    
           
       int  numRowsAffected  =  command.ExecuteNonQuery();    
           
       connection.Close();    
     
    ---------------------------------------------------------------  
     
    show  it  :    
     
     private  void  Page_Load(object  sender,  System.EventArgs  e)    
           
       {    
           
       string  imgid  =Request.QueryString["imgid"];    
           
       string  connstr=((NameValueCollection)    
           
       Context.GetConfig("appSettings"))["connstr"];    
           
       string  sql="SELECT  imgdata,  imgtype  FROM  ImageStore  WHERE  id  =  "    
           
       +  imgid;    
           
       SqlConnection  connection  =  new  SqlConnection(connstr);    
           
       SqlCommand  command  =  new  SqlCommand(sql,  connection);    
           
       connection.Open();    
           
       SqlDataReader  dr  =  command.ExecuteReader();    
           
       if(dr.Read())    
           
       {    
           
         Response.ContentType  =  dr["imgtype"].ToString();    
           
         Response.BinaryWrite(  (byte[])  dr["imgdata"]  );    
           
       }    
           
       connection.Close();    
           
       }
      

  2.   

    http://dotnet.aspx.cc/article/9154bc99-df64-4e2d-b096-26c99ce464be/read.aspx
      

  3.   

    {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.InitialDirectory = "c:\\";
                openfile.Filter = "All files (*.*)|*.*";
                openfile.FilterIndex = 1;
                openfile.RestoreDirectory = true;
                String FileName = "";
                if (openfile.ShowDialog() == DialogResult.OK)
                {
                    FileName = openfile.FileName;            }
                FileStream objFileStream;
                StreamReader objStreamReader;
                string strLine = "";            objFileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                byte[] fileData = new byte[objFileStream.Length];
                //把文件流填充到数组
                objFileStream.Read(fileData, 0, fileData.Length);    
                connection.updateSqlforImage(" update sb set picture=" + "@Image where id=2", fileData);
                objFileStream.Close();
              
            }
      

  4.   

    DataTable dt=connection.selectSql("select picture from sb where id=2");
                Byte[] byteBLOBData = new Byte[0];
                byteBLOBData = (Byte[])dt.Rows[0][0];
                MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                pictureBox1.Image = Image.FromStream(stmBLOBData);