怎样将图片读为二进制文件存入Access数据库?

解决方案 »

  1.   

    我以前使用VB将图片存到access数据库里,后来才知都是这种方式.就图片转成二进制流操作就可以解决了.
      

  2.   

    private byte[] LoadLogoData()
    {
    //得到提交的文件
    Stream fileDataStream = this.txtSiteLogo.PostedFile.InputStream;

    //得到文件大小
    int fileLength = this.txtSiteLogo.PostedFile.ContentLength;
    //创建数组
    byte[] fileData= new byte[fileLength]; fileDataStream.Read(fileData,0,fileLength); return fileData;
    }
      

  3.   

    先将图片转换成二进制流,然后在SQL语句中用参数的形式存入数据库
    //图片保存/读取支持 
    //ImageToByte(Image img) 将图片转换成二进制代码,然后存储数据库中
    public static byte[] ImageToByte(Image img)
    {
    byte[] byt = null;
    ImageConverter imgCvt = new ImageConverter();
    object obj = imgCvt.ConvertTo(img, typeof(byte[]));
    byt = (byte[])obj;
    return byt;
    } //ByteToImage(byte[] byt)读取数据库中的二进制文件将其转换为Image
    public static Image ByteToImage(byte[] bytImage)
    {
    Image img = null;
    ImageConverter imgCvt = new ImageConverter(); object obj = imgCvt.ConvertFrom(bytImage);
    img = (Image)obj;
    return img;
    } //插入参数
    comm.Parameters.Add("@GdsPhoto", SqlDbType.Image);
    comm.Parameters["@GdsPhoto"].Value = byt;
      

  4.   

    http://chang110cn.bokee.com/2563878.html
    这个传到SQL中的,改一下就OK了.
      

  5.   

    private void menuItem5_Click(object sender, System.EventArgs e)//图片加载
    {
    OpenFileDialog of=new OpenFileDialog();
    FileStream fs;
    of.Filter="Picture Files(*.jpg)|*.jpg|Bmp(*.bmp)|*.bmp|All Files(*.*)|*.*";
    if(of.ShowDialog()==DialogResult.OK&&of.FileName!="")
    {
    fs=new FileStream(of.FileName,FileMode.Open,FileAccess.Read);
    byte[] btyPic=new byte[(int)fs.Length];
    fs.Read(btyPic,0,btyPic.Length);
    try
    {
    this.ptbImage.SizeMode=PictureBoxSizeMode.Normal;
    this.ptbImage.Image=Image.FromFile(of.FileName);
    }
    catch(Exception ImageExp)
    {
    MessageBox.Show(ImageExp.Message);
    }
    fs.Close();
    System.Data.SqlClient.SqlCommand sqlUpdateCommand11=new System.Data.SqlClient.SqlCommand();
    sqlUpdateCommand11.Connection = this.sqlCon;
    sqlUpdateCommand11.CommandText = "Update JBDA set zp=@zp where zgbh=@zgbh";  

    sqlUpdateCommand11.Parameters.Add(new System.Data.SqlClient.SqlParameter("@zgbh", System.Data.SqlDbType.VarChar, 50, "zgbh"));
    sqlUpdateCommand11.Parameters["@zgbh"].Value=this.txtzgbh.Text;
    sqlUpdateCommand11.Parameters.Add(new System.Data.SqlClient.SqlParameter("@zp", System.Data.SqlDbType.Image,16, "zp"));
    sqlUpdateCommand11.Parameters["@zp"].Value=btyPic;
    try{sqlUpdateCommand11.ExecuteNonQuery();}
    catch(Exception exp)
    {MessageBox.Show(exp.Message);}
    this.sqljbda.Update(this.dsResult,"JBDA");
    this.dsResult.Clear();
    this.sqljbda.Fill(this.dsResult,"JBDA");

    //sqlUpdateCommand11.Connection.Close();
    MessageBox.Show("图片插入成功");

    }
    }
      

  6.   

    http://singlepine.cnblogs.com/articles/288027.html