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

从Oracle数据库中读出图片

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

Dim conn As New OracleClient.OracleConnection

Dim cmd As New OracleClient.OracleCommand

Dim myReader As OracleClient.OracleDataReader

Dim sql As String

Dim fl As File

sql = "select * from news where newsid=3211" '从数据库中取出图片数据 blob

conn.ConnectionString = "Password=macro;User ID=gf;Data Source=site"

cmd.CommandText = sql

cmd.Connection = conn

conn.Open()

myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)

myReader.Read()

Label1.Text = myReader("title")

Dim fs As FileStream ' Writes the BLOB to a file (*.bmp).

Dim bw As BinaryWriter ' Streams the binary data to the FileStream object.

Dim bufferSize As Integer = 1000 ' The size of the BLOB buffer.

Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte() buffer to be filled by GetBytes.

Dim retval As Long ' The bytes returned from GetBytes.

Dim startIndex As Long = 0 ' The starting position in the BLOB output.

Dim fpath As String

'将图片数据存成本地文件

fpath = Server.MapPath(Request.ApplicationPath) & "Imagephoto.bmp"

fs = New FileStream(fpath, FileMode.OpenOrCreate, FileAccess.Write)

bw = New BinaryWriter(fs)

' Reset the starting byte for a new BLOB.

startIndex = 0

' Read bytes into outbyte() and retain the number of bytes returned.

retval = myReader.GetBytes(2, startIndex, outbyte, 0, bufferSize) 'GetBytes返回字段中的可用字节数,第一个参数为从0开始的列序号

' Continue reading and writing while there are bytes beyond the size of the buffer.

Do While retval = bufferSize

bw.Write(outbyte)

bw.Flush() '清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。

' Reposition the start index to the end of the last buffer and fill the buffer.

startIndex = startIndex + bufferSize

retval = myReader.GetBytes(2, startIndex, outbyte, 0, bufferSize)

Loop

' Write the remaining buffer.

bw.Write(outbyte)

bw.Flush()

' Close the output file.

bw.Close()

fs.Close()

' Close the reader and the connection.

myReader.Close()

conn.Close()

Dim logo As Image '文件格式转换

logo = Image.FromFile(fpath)

fpath = Server.MapPath(Request.ApplicationPath) & "Imagezkjhy.jpeg"

logo.Save(fpath, System.Drawing.Imaging.ImageFormat.Jpeg)

'Me.Image1.Width = New Unit(300) '调整显示大小

'Me.Image1.Height = New Unit(350)

Me.Image1.ImageUrl = "image/zkjhy.jpeg"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值