C#从数据库读取PDF文件 问题
以二进制存入PDF到数据库代码:FileStreammyStream=newFileStream(Filename,FileMode.Open,FileAccess.Re...
以二进制存入PDF到数据库代码: FileStream myStream = new FileStream(Filename,FileMode.Open ,FileAccess.Read ); Byte[] imageBody =new byte[myStream.Length]; myStream.Read(imageBody,0,(int)myStream.Length); myStream.Close() ; string sqlstr= "Insert into pubmed values( "ID ", ' "+imageBody+ " ') "; try { SqlCommand cm=new SqlCommand(sqlstr,myConnection); cm.ExecuteNonQuery(); cm.Dispose(); MessageBox.Show( "上传成功! "); } catch(Exception er) { MessageBox.Show(er.Message); } 以二进制读取数据库中PDF代码: MemoryStream ms; SqlDataReader myReader; string sqlstr= "select top 1 * from pubmed where id=2 "; SqlCommand cm=new SqlCommand(sqlstr,myConnection); try { byte[] imageBody=(byte[])cm.ExecuteScalar(); ms=new MemoryStream(imageBody); this.pictureBox1.Image=Image.FromStream(ms); } catch(Exception er) { MessageBox.Show(er.Message); } cm.Dispose(); 我上传PDF文件是可以,但是再读取的时候就提示, "无法进行格式转换 ", 我该如何显示PDF文件
展开
1个回答
展开全部
private SqlDataAdapter dataAdapter; // 数据适配器 private DataSet dataSet; string sql = ("select photo from StuImage where num='200840410201'" dataAdapter = new SqlDataAdapter(sql, DBHelp.connection); dataSet = new DataSet("photo "); dataAdapter.Fill(dataSet, "photo "); FileStream myFileStream; MemoryStream myMemoryStream = new MemoryStream((byte[])dataSet.Tables[0].Rows[0]["photo "]); myFileStream = new FileStream(@"C:\Documents and Settings\Administrator\桌面\1.jsp", FileMode.Create, FileAccess.Write); BinaryWriter myBinaryWriter = new BinaryWriter(myFileStream); byte[] aBtEntityFile = myMemoryStream.ToArray(); myBinaryWriter.Write(aBtEntityFile); myBinaryWriter.Close(); myFileStream.Close(); myMemoryStream.Close(); 你的二进制存入就有问题。。我给你这个例子 你可以参考
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询