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 |
|
4 |
No |
|
Description |
|
50 |
Yes |
|
BlobData |
|
16 |
Yes |
The sample code contains nine event handlers:
Form.LoadCreates a
DataAdapterto read and update the Id and Description fields from table TBL0911. ATextBoxis bound to the Id column and anotherTextBoxis bound to the Description field. ADataSetis filled with all records from TBL0911. TheBindingManageris retrieved for the table in theDataSet. A handler is attached to theBindingManager.PositionChangedevent. Finally, the display is updated for the current record in the table.BindingManagerBase.PositionChangedClears the image displayed in the
PictureBoxon 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. Theimageis retrieved using aDataReader. AMemoryStreamis created from the image retrieved and theMemoryStreamis loaded into thePictureBoxusing theImage.FromStream( )method passing the image in theMemoryStreamas an argument.- Select Image
Button.Click Opens a file dialog to allow an image to be selected by the user. The
imageis retrieved using aFileStream ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access