O'Reilly logo

ADO.NET Cookbook by Bill Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.11. Reading and Writing Binary Data with SQL Server

Problem

You need to read and write binary data from and to a SQL Server 2000 database.

Solution

Use the techniques from the following example.

The schema of table TBL0911 used in this solution is shown in Table 9-3.

Table 9-3. TBL0911 schema

Column name

Data type

Length

Allow nulls?

Id

int

4

No

Description

nvarchar

50

Yes

BlobData

image

16

Yes

The sample code contains nine event handlers:

Form.Load

Creates a DataAdapter to read and update the Id and Description fields from table TBL0911. A TextBox is bound to the Id column and another TextBox is bound to the Description field. A DataSet is filled with all records from TBL0911. The BindingManager is retrieved for the table in the DataSet. A handler is attached to the BindingManager.PositionChanged event. Finally, the display is updated for the current record in the table.

BindingManagerBase.PositionChanged

Clears the image displayed in the PictureBox on the form. The ID of the current record is retrieved. A connection is created to select the field BlobData—an image—from TBL0911 corresponding to the current record. The image is retrieved using a DataReader. A MemoryStream is created from the image retrieved and the MemoryStream is loaded into the PictureBox using the Image.FromStream( ) method passing the image in the MemoryStream as an argument.

Select Image Button.Click

Opens a file dialog to allow an image to be selected by the user. The image is retrieved using a FileStream ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required