设为首页 收藏本站
查看: 488|回复: 0

[经验分享] 存入图片至ORACLE及从ORACLE读取图片方法整理

[复制链接]

尚未签到

发表于 2016-8-4 12:29:09 | 显示全部楼层 |阅读模式
  一、将图片存入Oracle数据库
  示例表NEWS的结构为:newsid number(10),titlevarchar2(100),image(blob)
  方法1:利用OracleCommandBuilder类(该类用于自动生成用于协调 DataSet 的更改与关联的数据库的单表命令。)
  Dim cn As New OracleConnection("data source=site;uid=gf;pwd=macro;")
cn.Open()
  Dim da As New OracleDataAdapter("select * from news", cn)
Dim myCB As New OracleCommandBuilder(da)
Dim ds As New DataSet("news")
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim fs As Stream = File1.PostedFile.InputStream'File1为文件选择框,作为服务器控件使用
Dim mydata(fs.Length) As Byte
fs.Read(mydata, 0, fs.Length)
fs.Close()
da.Fill(ds, "news")
Dim myRow As DataRow = ds.Tables("news").NewRow
myRow("newsid") = txtNewsID.Text
myRow("title") = txtTitle.Text
myRow("image") = mydata
ds.Tables("news").Rows.Add(myRow)
da.Update(ds, "news")
  cn.Close()
  方法2:利用过程
  事先定义的Oracle过程为:
  CREATE OR REPLACE PROCEDURE "GF"."NEWS_ADD"
(in_newsid in
news.newsid%type,
in_title in news.title%type,
in_imag in news.image%type)
as
begin
insert into gf.news values(in_newsid,in_title,in_image);
end;
  下面是将数据存入数据库的代码:
  Dim cn As New OracleConnection("data source=site;uid=gf;pwd=macro;")
cn.Open()
  Dim cmd As New OracleCommand("news_add", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New OracleParameter("in_newsid", OracleType.Number, 10))
cmd.Parameters("in_newsid").Value = txtNewsID.Text.Trim
cmd.Parameters.Add(New OracleParameter("in_title", OracleType.VarChar, 100))
cmd.Parameters("in_title").Value = txtTitle.Text.Trim
  Dim fs As Stream = File1.PostedFile.InputStream
Dim mydata(fs.Length) As Byte '定义一个字节数组用于读取图片流
fs.Read(mydata, 0, fs.Length)
fs.Close()
  cmd.Parameters.Add(New OracleParameter("in_image", OracleType.Blob))
cmd.Parameters("in_image").Value = mydata
  cmd.ExecuteNonQuery()
  cn.Close()
  二、从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) & "/Image/photo.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) & "/Image/zkjhy.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"

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-252730-1-1.html 上篇帖子: oracle 执行计划的初试 利用Oracle执行计划机制提高查询性能 下篇帖子: 【ORACLE】Oracle数据库添加表空间,添加用户,设置权限,删除用户
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表