国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

利用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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 徐水县| 商河县| 登封市| 准格尔旗| 女性| 南京市| 临泽县| 广东省| 阿勒泰市| 武川县| 德昌县| 黄平县| 南川市| 富川| 鸡西市| 古丈县| 阳朔县| 麻城市| 绍兴县| 石家庄市| 龙泉市| 和田县| 武安市| 建水县| 新郑市| 长顺县| 诏安县| 三江| 乌审旗| 永福县| 奉化市| 珠海市| 武城县| 都江堰市| 云龙县| 喀喇沁旗| 光山县| 隆子县| 昌都县| 维西| 石景山区|