Microsoft KB Archive/320629

= HOW TO: Read and Write BLOB Data by Using ADO.NET with Visual J# .NET =

Article ID: 320629

Article Last Modified on 9/8/2003

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 1.1
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



This article was previously published under Q320629



For a Microsoft Visual C# .NET version of this article, see 309158.

For a Microsoft Visual Basic .NET version of this article, see 308042.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data.SqlClient.*
 * System.IO.*

IN THIS TASK
SUMMARY
 * Requirements
 * Create the Project



SUMMARY
The GetChunk and the AppendChunk methods are not available in ADO.NET on DataReader columns, DataSet columns, or Command parameters. This article describes how to use Visual J# .NET to read and write binary large object (BLOB) fields.

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 Visual J# .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 &quot;ID&quot; of type Int. Field that is named &quot;Descp&quot; of type VarChar with a length of 50. Field that is named &quot;ImgField&quot; of type Image.

 Start Visual Studio .NET, and then create a new Visual J# Windows Application project. Drag two Button controls from the toolbox to the default form, Form1.</li> 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) .</li>  Add the following code to the top of the Code window, just below the package statement: import System.Data.SqlClient.*; import System.IO.*; </li>  Double-click Button1, and then add the following code to the Button1_Click event handler: SqlConnection con = new SqlConnection(&quot;Server=server;User ID=uid;Password=pwd;Database=Northwind&quot;); SqlDataAdapter da = new SqlDataAdapter(&quot;Select * From MyImages&quot;, con); SqlCommandBuilder MyCB = new SqlCommandBuilder(da); DataSet ds = new DataSet; da.set_MissingSchemaAction(MissingSchemaAction.AddWithKey);

FileStream fs = new FileStream(&quot;C:\\winnt\\Gone Fishing.BMP&quot;, FileMode.OpenOrCreate, FileAccess.Read); ubyte[] MyData = new ubyte[System.Convert.ToInt32(fs.get_Length)]; fs.Read(MyData, 0, System.Convert.ToInt32(fs.get_Length)); fs.Close; da.Fill(ds,&quot;MyImages&quot;); DataRow myRow; myRow = ds.get_Tables.get_Item(&quot;MyImages&quot;).NewRow; myRow.set_Item(&quot;Descp&quot;,(System.String)&quot;This would be description text&quot;); myRow.set_Item(&quot;imgField&quot;,MyData); ds.get_Tables.get_Item(&quot;MyImages&quot;).get_Rows.Add(myRow);

da.Update(ds, &quot;MyImages&quot;); con.Close; </li>  Double-click Button2, and then add the following code to the Button2_Click event handler: SqlConnection con = new SqlConnection(&quot;Server=server;User ID=uid;Password=pwd;Database=Northwind&quot;); SqlDataAdapter da = new SqlDataAdapter(&quot;Select * From MyImages&quot;, con); SqlCommandBuilder MyCB = new SqlCommandBuilder(da); DataSet ds = new DataSet;

ubyte[] MyData= new ubyte[0]; da.Fill(ds, &quot;MyImages&quot;);

DataRow myRow; myRow = ds.get_Tables.get_Item(&quot;MyImages&quot;).get_Rows.get_Item(0); MyData = (ubyte[])myRow.get_Item(&quot;imgField&quot;); int ArraySize; ArraySize = MyData.GetUpperBound(0);

FileStream fs = new FileStream(&quot;C:\\winnt\\Gone Fishing2.BMP&quot;, FileMode.OpenOrCreate, FileAccess.Write); fs.Write(MyData, 0,ArraySize); fs.Close; </li> Modify the parameters in the connection strings to connect to the computer that is running SQL Server.</li> Press F5 to compile and to run the application.</li> Click Save to Database (from File) to load the image, C:\WinNT\Gone Fishing.bmp, into the SQL Server Image field. If this file does not exist on your computer, modify the code so that it refers to an existing file.</li> Click Save to File (from Database) to save the data from the SQL Server Image field back to a file.</li></ol>

back to the top

Additional query words: JSharp VJ#

Keywords: kbhowtomaster kbio kbsqlclient kbsystemdata KB320629

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.