1. |
Create a SQL Server or Access table with the following structure:CREATE TABLE BLOBTest
(
BLOBID INT IDENTITY NOT NULL,
BLOBData IMAGE NOT NULL
)
|
2. |
Start Visual Studio .NET, and then create a new Visual Basic Windows Forms application. |
3. |
Drag a PictureBox control and two Button controls from the toolbox to the default Form1. Set the Text property of Button1 to File to Database, and then set the Text property of Button2 to Database to PictureBox. |
4. |
Add the following Imports statements at the top of the form's code module:Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging
|
5. |
Add the following declaration for the database connection string below Public Class Form1, and then modify the connection string as necessary for your environment: Dim strCn As String = "Data Source=<server>;" & _
"Initial Catalog=<database>;Integrated Security=SSPI"
|
6. |
Add the following code in the Click event procedure of Button1 (File to Database). Modify the file path to an available sample image file as necessary. This code reads the image file from disk (by using a FileStream object) into a Byte array, and then inserts the data into the database by using a parameterized Command object. Dim cn As New SqlConnection(strCn)
Dim cmd As New SqlCommand("INSERT INTO BLOBTest (BLOBData) " & _
"VALUES (@BLOBData)", cn)
Dim strBLOBFilePath As String = _
"C:\Documents and Settings\All Users\Documents" & _
"\My Pictures\Sample Pictures\winter.jpg"
Dim fsBLOBFile As New FileStream(strBLOBFilePath, _
FileMode.Open, FileAccess.Read)
Dim bytBLOBData(fsBLOBFile.Length() - 1) As Byte
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
fsBLOBFile.Close()
Dim prm As New SqlParameter("@BLOBData", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
cmd.Parameters.Add(prm)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
|
7. |
Add the following code in the Click event procedure of Button2 (Database to PictureBox). This code retrieves the rows from the BLOBTest table in the database into a DataSet, copies the most recently added image into a Byte array and then into a MemoryStream object, and then loads the MemoryStream into the Image property of the PictureBox control. Dim cn As New SqlConnection(strCn)
Dim cmd As New SqlCommand("SELECT BLOBID, " & _
"BLOBData FROM BLOBTest ORDER BY BLOBID", cn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "BLOBTest")
Dim c As Integer = ds.Tables("BLOBTest").Rows.Count
If c > 0 Then
Dim bytBLOBData() As Byte = _
ds.Tables("BLOBTest").Rows(c - 1)("BLOBData")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
picBLOB.Image = Image.FromStream(stmBLOBData)
End If
|
8. |
Run the project. |
9. |
Click File to Database to load at least one sample image into the database. |
10. |
Click Database to PictureBox to display the saved image in the PictureBox control. |
11. |
If you want to be able to insert the image from the PictureBox control directly into the database, add a third Button control, and then add the following code in its Click event procedure. This code retrieves the image data from the PictureBox control into a MemoryStream object, copies the MemoryStream into a Byte array, and then saves the Byte array to the database by using a parameterized Command object. Dim cn As New SqlConnection(strCn)
Dim cmd As New SqlCommand("INSERT INTO BLOBTest (BLOBData) " & _
"VALUES (@BLOBData)", cn)
Dim ms As MemoryStream = New MemoryStream()
picBLOB.Image.Save(ms, ImageFormat.Jpeg)
Dim bytBLOBData(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(bytBLOBData, 0, ms.Length)
Dim prm As New SqlParameter("@BLOBData", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
cmd.Parameters.Add(prm)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
|
评论