利用DataSet存取SQL Server中的二進制文件
2024-07-21 02:23:42
供稿:網友
利用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