How To Read and Write BLOB Data by Using ADO.NET with Visual Basic .NET (308042)
The information in this article applies to:
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft ADO.NET (included with the .NET Framework 1.1)
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual Basic .NET (2003)
This article was previously published under Q308042 For a Microsoft Visual C# .NET version of this
article, see
309158. This article refers to the
following Microsoft .NET Framework Class Library namespaces:
- System.Data.SqlClient
- System.IO
IN THIS TASKSUMMARY The GetChunk and the AppendChunk methods are not available in ADO.NET to read and write binary
large object (BLOB) fields. This article describes how to use the FileStream object and a byte array to read and to write BLOB data from
Microsoft SQL Server to a file.
back to the top
Requirements The following list outlines the recommended hardware, software,
network infrastructure, and service packs that are required:
- Microsoft Windows 2000 Professional, Windows 2000 Server,
Windows 2000 Advanced Server, or Windows NT 4.0 Server
- Microsoft Visual Studio .NET
- Microsoft SQL Server
back to the top
Create the Project- Add a table named MyImages to your SQL Server Northwind database. Include the following fields in your table:
- Identity field that is named "ID" of type Int.
- Field that is named "Description" of type VarChar with a length of 50.
- Field that is named "ImgField" of type Image.
- Start Visual Studio .NET, and then create a new Visual
Basic Windows Application project.
- Add two Button controls to the default form, Form1.
- In the Properties window, change the Text property of Button1 to Save to Database (from File), and then
change the Text property of Button2 to Save to File (from
Database).
- Add the following code to the top of the Code window:
Imports System.Data.SqlClient
Imports System.IO
- Double-click Button1, and then add the following code to the Button1_Click event handler:
Note You must change uid<username> and pwd
=<strong password> to the correct values before you run this code. Make
sure that User ID has the appropriate permissions to perform this operation on
the database.
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")
- Double-click Button2, and then add the following code to the Button2_Click event handler:
Note You must change uid <username> and
pwd =<strong password> to the correct values before you run this code. Make
sure that User ID has the appropriate permissions to perform this operation on
the database.
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")
- Press F5 to compile and to run the application.
- Click Save to Database (from File) to load
the image, C:\WinNT\Gone Fishing.bmp, into the SQL Server Image field. After you receive the confirmation message that the image
has been saved, check your table to verify.
- Click Save to File (from Database) to save
the data from the SQL Server Image field back to a file. Verify that C:\WinNT\Gone Fishing2.bmp now
exists.
back to the top
REFERENCES For additional
information about a similar topic in Visual Basic 6.0, click the following
article number to view the article in the Microsoft Knowledge Base: 258038
How To Access and Modify SQL Server BLOB Data by Using the ADO Stream Object
For more general information about ADO.NET or Visual
Basic .NET, refer to the following MSDN newsgroups:
back to the top
Modification Type: | Minor | Last Reviewed: | 7/14/2004 |
---|
Keywords: | kbHOWTOmaster kbIO kbSqlClient kbSystemData KB308042 kbAudDeveloper |
---|
|