欢迎光临
我们一直在努力

利用DataSet存取SQL Server中的二进制文件-.NET教程,数据库应用

建站超值云服务器,限时71元/月

利用dataset存取sql server中的二进制文件

作者 朱二

利用dataset可以方便的对sql server中的二进制文件进行存取与更新操作,下面是详细的代码演示

演示环境:

数据库机器名 :s_test

登陆名 :sa

密码 :7890

数据库名 db_test

下面建立一个表:

create table tb_test(id int identity(1,1),photo image ,constraint pk_tb_test primary key(id))

一、将硬盘上的文件保存至数据库(vb.net)

———————————————————-

———————————————————-

下面的示例将c:\1.jpg文件保存至数据库的tb_test表中

———————————————————-

———————————————————-

imports system.io

imports system.data.sqlclient

public class image

shared sub main()

读入文件数据

dim fs = new filestream("c:\1.jpg", io.filemode.open, io.fileaccess.read)

dim imgdata(fs.length – 1) as byte

fs.read(imgdata, 0, fs.length – 1)

fs.close()

dim tempconnection as new sqlconnection

dim tempadapter as sqldataadapter

dim tempdataset as new dataset

打开数据库连接

tempconnection.connectionstring = "server=s_test;uid=sa;pwd=7890;database=db_test"

tempconnection.open()

tempadapter = new sqldataadapter("select * from tb_test where 1=0", tempconnection)

dim cb as new sqlcommandbuilder(tempadapter)

tempadapter.fill(tempdataset)

插入一条记录

dim tempdatarow as datarow

tempdatarow = tempdataset.tables(0).newrow()

tempdatarow("photo") = imgdata

tempdataset.tables(0).rows.add(tempdatarow)

tempadapter.update(tempdataset)

tempconnection.close()

end sub

end class

二、将数据库中的文件保存至硬盘(vb.net)

———————————————————-

———————————————————-

下面的示例将数据库的tb_test表中第一条记录的photo保存至c:\2.jpg

———————————————————-

———————————————————-

imports system.io

imports system.data.sqlclient

public class image

shared sub main()

dim tempconnection as new sqlconnection

dim tempadapter as sqldataadapter

dim tempdataset as new dataset

打开数据库连接,取出数据

tempconnection.connectionstring = "server=s_test;uid=sa;pwd=7890;database=db_test"

tempconnection.open()

tempadapter = new sqldataadapter("select top 1 * from tb_test", tempconnection)

tempadapter.fill(tempdataset)

tempconnection.close()

if tempdataset.tables(0).rows.count > 0 then

将文件保存到硬盘文件c:\2.jpg

dim imgdata() as byte

imgdata = tempdataset.tables(0).rows(0).item("photo")

dim fs as filestream

fs = file.create("c:\2.jpg", imgdata.length – 1)

fs.write(imgdata, 0, imgdata.length – 1)

fs.close()

end if

end sub

end class

三、更新数据库中保存的文件

———————————————————-

———————————————————-

下面的示例用将数据库的tb_test表中第一条记录的photo更新为c:\2.jpg

———————————————————-

———————————————————-

imports system.io

imports system.data.sqlclient

public class image

shared sub main()

读取文件

dim fs = new system.io.filestream("c:\2.jpg", io.filemode.open, io.fileaccess.read)

dim imgdata(fs.length – 1) as byte

fs.read(imgdata, 0, fs.length – 1)

fs.close()

dim tempconnection as new sqlconnection

dim tempadapter as sqldataadapter

dim tempdataset as new dataset

打开数据库连接,取出数据

tempconnection.connectionstring = "server=s_test;uid=sa;pwd=7890;database=db_test"

tempconnection.open()

tempadapter = new sqldataadapter("select top 1 * from tb_test", tempconnection)

tempadapter.fill(tempdataset)

更新数据

dim cb as new sqlcommandbuilder(tempadapter)

tempdataset = new dataset

tempadapter.fill(tempdataset)

tempdataset.tables(0).rows(0).item("photo") = imgdata

tempadapter.update(tempdataset)

tempconnection.close()

end sub

end class

总结:

利用dataset可以方便的对sql server中的二进制文件进行存取与更新操作,虽不是最有效的方法,但通过文本的介绍,可使初学者多掌握一种对 数据库中的二进制文件进行操作的方法,希望对开发者有所帮助。

vvvv

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用DataSet存取SQL Server中的二进制文件-.NET教程,数据库应用
分享到: 更多 (0)

相关推荐

  • 暂无文章