GetChunk 和
AppendChunk 方法在 ADO.NET 中不可用以读写二进制大对象 (BLOB) 字段。本文介绍如何使用
FileStream 对象和字节数组从 Microsoft SQL Server 读取 BLOB 数据并将其写入文件。
要求
下表列出了推荐使用的硬件、软件、网络基础结构以及所需的 Service Pack:
- Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server 或 Windows NT 4.0 Server
- Microsoft Visual Studio .NET
- Microsoft SQL Server
创建项目
- 向您的 SQL Server Northwind 数据库添加一个名为“MyImages”的表。在该表中包含以下字段:
- 名称为“ID”,类型为 Int 的标识字段。
- 名称为“Description”,类型为 VarChar,长度为 50 的字段。
- 名称为“ImgField”,类型为 Image 的字段。
- 启动 Visual Studio .NET,然后新建一个 Visual Basic Windows 应用程序项目。
- 向默认窗体 Form1 中添加两个 Button 控件。
- 在“属性”窗口中,将 Button1 的 Text 属性更改为保存到数据库(从文件),然后,将 Button2 的 Text 属性更改为保存到文件(从数据库)。
- 将下面的代码添加到代码窗口的顶部:
Imports System.Data.SqlClient
Imports System.IO
- 双击 Button1,然后将以下代码添加到 Button1_Click 事件处理程序中:
注意:您必须先将 uid<username> 和 pwd =<strong password> 更改为正确的值,然后才能运行此代码。请确保该用户 ID 具有在数据库中执行此操作所需的适当权限。
Dim con As New SqlConnection _
("Server=YourServer;uid=<username>;pwd=<strong password>;database=northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim fs As New FileStream _
("C:\winnt\Gone Fishing.BMP", FileMode.OpenOrCreate, _
FileAccess.Read)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
con.Open()
da.Fill(ds, "MyImages")
Dim myRow As DataRow
myRow = ds.Tables("MyImages").NewRow()
myRow("Description") = "This would be description text"
myRow("imgField") = MyData
ds.Tables("MyImages").Rows.Add(myRow)
da.Update(ds, "MyImages")
fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing
con.Close()
con = Nothing
MsgBox ("Image saved to database")
- 双击 Button2 然后将下列代码添加到 Button2_Click 事件处理程序:
注意:您必须先将 uid<username> 和 pwd =<strong password> 更改为正确的值,然后才能运行此代码。请确保该用户 ID 具有在数据库中执行此操作所需的适当权限。
Dim con As New SqlConnection _
("Server=YourServer;uid=<username>;pwd=<strong password>;database=northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()
con.Open()
da.Fill(ds, "MyImages")
Dim myRow As DataRow
myRow = ds.Tables("MyImages").Rows(0)
Dim MyData() As Byte
MyData = myRow("imgField")
Dim K As Long
K = UBound(MyData)
Dim fs As New FileStream _
("C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, _
FileAccess.Write)
fs.Write(MyData, 0, K)
fs.Close()
fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing
con.Close()
con = Nothing
MsgBox ("Image retrieved")
- 按 F5 编译并运行该应用程序。
- 单击“保存到数据库(从文件)”以将图像 C:\WinNT\Gone Fishing.bmp 加载到 SQL Server 中的图像字段。收到确认图像已保存的信息后,请核对您的表进行验证。
- 单击“保存到文件(从数据库)”以将 SQL Server 图像字段中的数据保存回一个文件中。检查 C:\WinNT\Gone Fishing2.bmp 此时是否存在。
如想了解在使用 Visual Basic 6.0 时关于类似主题的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
258038
(http://support.microsoft.com/kb/258038/
)
如何使用 ADO Stream 对象访问并修改 SQL Server BLOB 数据
有关 ADO.NET 或 Visual Basic .NET 的更多常规信息,请访问以下 MSDN 新闻组:
文章编号: 308042 - 最后修改: 2004年6月29日 - 修订: 4.0
这篇文章中的信息适用于:
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft ADO.NET 1.1
- Microsoft Visual .NET 2002 标准版
- Microsoft Visual Basic .NET 2003 标准版
| kbhowtomaster kbio kbsqlclient kbsystemdata KB308042 |
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。