电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 2004 人浏览分享

从Oracle数据库中读出图片的方法(代码教程)

[复制链接]
2004 0
从Oracle数据库中读出图片

方法1:从数据库中读取数据并以文件形式保留在服务器上



  1. Dim conn As New OracleClient.OracleConnection
  2.        Dim cmd As New OracleClient.OracleCommand
  3.        Dim myReader As OracleClient.OracleDataReader
  4.        Dim sql As String
  5.        Dim fl As File

  6.        sql = "select * from news where newsid=3211"  '从数据库中取出图片数据 blob
  7.        conn.ConnectionString = "Password=macro;User ID=gf;Data Source=site"
  8.        cmd.CommandText = sql
  9.        cmd.Connection = conn

  10.        conn.Open()
  11.        myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
  12.        myReader.Read()

  13.        Label1.Text = myReader("title")

  14.        Dim fs As FileStream                 ' Writes the BLOB to a file (*.bmp).
  15.        Dim bw As BinaryWriter               ' Streams the binary data to the FileStream object.
  16.        Dim bufferSize As Integer = 1000      ' The size of the BLOB buffer.
  17.        Dim outbyte(bufferSize - 1) As Byte  ' The BLOB byte() buffer to be filled by GetBytes.
  18.        Dim retval As Long                   ' The bytes returned from GetBytes.
  19.        Dim startIndex As Long = 0           ' The starting position in the BLOB output.        
  20.        Dim fpath As String

  21.        '将图片数据存成本地文件
  22.        fpath = Server.MapPath(Request.ApplicationPath) & "\Image\photo.bmp"
  23.        fs = New FileStream(fpath, FileMode.OpenOrCreate, FileAccess.Write)
  24.        bw = New BinaryWriter(fs)

  25.        ' Reset the starting byte for a new BLOB.
  26.        startIndex = 0

  27.        ' Read bytes into outbyte() and retain the number of bytes returned.
  28.        retval = myReader.GetBytes(2, startIndex, outbyte, 0, bufferSize) 'GetBytes
  29. 返回字段中的可用字节数,第一个参数为从0开始的列序号

  30.        ' Continue reading and writing while there are bytes beyond the size of the buffer.
  31.        Do While retval = bufferSize
  32.            bw.Write(outbyte)
  33.            bw.Flush() '清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。

  34.            ' Reposition the start index to the end of the last buffer and fill the buffer.
  35.            startIndex = startIndex + bufferSize
  36.            retval = myReader.GetBytes(2, startIndex, outbyte, 0, bufferSize)
  37.        Loop

  38.        ' Write the remaining buffer.
  39.        bw.Write(outbyte)
  40.        bw.Flush()

  41.        ' Close the output file.
  42.        bw.Close()
  43.        fs.Close()

  44.        ' Close the reader and the connection.
  45.        myReader.Close()
  46.        conn.Close()

  47.        Dim logo As Image               '文件格式转换
  48.        logo = Image.FromFile(fpath)
  49.        fpath = Server.MapPath(Request.ApplicationPath) & "\Image\zkjhy.jpeg"
  50.        logo.Save(fpath, System.Drawing.Imaging.ImageFormat.Jpeg)

  51.        'Me.Image1.Width = New Unit(300) '调整显示大小
  52.        'Me.Image1.Height = New Unit(350)
  53.        Me.Image1.ImageUrl = "image/zkjhy.jpeg"
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.