日期:2008-11-03  浏览次数:20568 次

利用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(temp