For Microsoft Visual Basic .NET versions of this article, see
317670.
This task content
Summary
Example defect reference
SUMMARY This step-by-step guide describes how to copy images stored in the database to a Windows form.
The PictureBox control does not need to save this image to the file.
In Microsoft Visual Basic 6.0, if you want to be directly
The only method is the only way to save images in the PictureBox control to file this intermediate step by saving the binary large object {blob) data.
PictureBox is bound to a data source such as ActiveX Data Objects (ADO) data control or recordset. If you do not save images to your file for use, there is no way (without using data binding) to programm to the control.
In this article, we will use
System.io group
MemoryStream object copies the image data directly from the database to
PictureBox control.
Back to top
The following table summarizes the recommended hardware, software, network structure, and the required service pack:
Installing Microsoft Visual Studio .NET in a Microsoft Windows operating system for testing available Microsoft SQL Server instances or available Microsoft Access Database This article assumes that you are familiar with the following topics:
Visual C # .NET Windows Forms Binary Big Objects (BLOB) Storage ADO.NET Data Access
Back to top
Example
Create a SQL Server or Access Table: Create Table BlobTest
(
Blobid IntTentity Not NULL,
Blobdata Image Not Null
)
Open Visual Studio .NET and create a Visual C # Windows application project. Add a Picturebox and two Button controls to the default Form1 from the toolbox. Set the TEXT property of Button1 to File to Database and set the button of Button2 to Database to PictureBox. Insert a USING statement at the top of the code module: use system.data.sqlclient;
Using system.io;
Using system.drawing.image;
Add the declaration of the following database connection string to the Public Class Form1: System.Windows.Forms.Form class declaration, adjust the connection string as needed: string strinc = "data source = localhost; integrated security = sspi; initial catalog = mydata ";
Insert the following code into the Click event of the file to database. Adjust the available path of an available sample image file as needed. This code reads the image file from the disk (using a FILESTREAM object) into the BYTE array, then insert the data into the database using a parameterized Command object. Try
{
SqlConnection CN = New SQLCONNECTION (STRCN);
Sqlcommand cmd = new SQLCOMMAND ("INSERT INTO BLOBTEST (BLOBDATA) VALUES (@Blobdata)", CN); String strblobfilepath = @ "c: / blue hills.jpg"; // modify this path as needed.
// read JPG INTO File Stream, And from there byte Array.
FILESTREAM FSBLOBFILE = New FileStream (strbrobfilepath, filemode.open, fileaccess.read);
Byte [] bytblobdata = new byte [fsblobfile.length];
Fsblobfile.read (bytblobdata, 0, bytblobdata.length);
fsblobfile.close ();
// Create Parameter for Insert Command and Add to SqlCommand Object.
Sqlparameter prm = new SQLParameter ("@ blobdata", sqldbtype.varbinary, bytblobdata.length, parameterdirection.input, false,
0, 0, NULL, DATAROWVERSION.CURRENT, BYTBLOBDATA);
Cmd.Parameters.Add (PRM);
// Open Connection, Execute Query, And Close Connection.
Cn.open ();
cmd.executenonquery ();
Cn.close ();
} catch (Exception EX)
{MessageBox.show (ex.Message);
Insert the following code into the Click Event Process of the Database to Picturebox. This code retrieves the BLOBTEST table from the database to a data set, copy the latest image to the Byte array, then to the MemoryStream object, then load MemoryStream to the Image property of the PictureBox control. Try
{
SqlConnection CN = New SQLCONNECTION (STRCN);
Cn.open ();
// Retrieve Blob from Database INTO DATASET.
Sqlcommand cmd = new SQLCommand ("Select Blobid, BlobData from Blobtest Order By Blobid", CN);
SqlDataAdapter Da = New SqlDataAdapter (CMD);
DataSet DS = New Dataset ();
Da.fill (DS, "blobtest");
INT C = DS.TABLES ["blobtest"]. rount.count;
IF (c> 0)
{// blob is read into byte array, the used to construct memorystream,
// Then Passed to Picturebox.
Byte [] byteblobdata = new byte [0];
BYTEBLOBDATA = (byte []) (DS.Tables ["blobtest"]. Rows [c - 1] ["blobdata"]); MemoryStream Stmblobdata = New MemoryStream (Byteblobdata);
Picturebox1.image = image.fromstream (stmblobdata);
}
Cn.close ();
}
Catch (Exception EX)
{MessageBox.show (ex.Message);
Press F5 to compile and run the project. Click the File to Database button to load at least one sample image into the database. Click the Database to Picturebox button to display the saved image in the PictureBox control. If you want to directly insert images from the PictureBox control into the database, add the third Button control and insert the following code into its Click event procedure. This code retrieves image data from the PictureBox control to the MemoryStream object, copies MemoryStream to a BYTE array, then uses a parameterized Command object to save the Byte array to the database. Try
{
SqlConnection CN = New SQLCONNECTION (STRCN);
SQLCommand cmd = new SQLCOMMAND ("INSERT INTO BLOBTEST (BLOBDATA) VALUES (@BlobData)", CN);
// Save Image from PictureBox Into MemoryStream Object.
MemoryStream MS = New MemoryStream ();
Picturebox1.image.save (ms, imageformat.jpeg);
// read from memoryStream Into Byte Array.
Byte [] bytblobdata = new byte [ms.length];
Ms.Position = 0;
Ms.read (ByTBlobData, 0, Convert.Toint32 (Ms.Length));
// CREATE Parameter for Insert Statement That Contains Image.
Sqlparameter prm = new SQLParameter ("@ blobdata", sqldbtype.varbinary, bytblobdata.length, parameterdirection.input, false,
0, 0, NULL, DATAROWVERSION.CURRENT, BYTBLOBDATA);
Cmd.Parameters.Add (PRM);
Cn.open ();
cmd.executenonquery ();
Cn.close ();
} catch (Exception EX)
{MessageBox.show (ex.Message);
Run the project. Click the Database to Picturebox button to display images that have been saved in the PictureBox control. Click the newly added button to save this image from the Picturebox to the database. Then click the Database to Picturebox button again to confirm that the image is saved correctly.
Back to top
defect
This test does not apply to the photo columns in the employee table of the Ross document repository in Access and SQL Server. The bitmap image stored in the photo column is packaged with the title information created by the Visual Basic 6.0 Ole Container control. If you need to use the Access database to test this code, you need to create an OLE Object type column in the Access table and use the System.Data.Data.OleDb namespace in Microsoft Jet 4.0 Provider instead of the System.Data.sqlclient namespace. Back to top
Refer to additional information about using BLOB data in Visual C # .NET, click the article number below to see the article in the Microsoft Knowledge Base:
309158 How to: Read and write BLOB data in Visual C # .NET by using ADO.NET
Back to top
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 C # .NET (2002) Microsoft Visual C # .NET (2003)
Recent Updated: 2003-11-4 (2.1) Keywords: KBDataBinding KbhowTomaster KB317701 KBAUDDEVELOPER