Lm th? no ? hi?n th? m?t h?nh ?nh t? m?t c s? d? li?u trong m?t c?a s? h?nh th?c PictureBox b?ng cch s? d?ng Visual Basic.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 321900 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi t?ng b?c ny m t? cch hi?n th? m?t h?nh ?nh m ?c lu tr? trong c s? d? li?u trong m?t PictureBox ki?m sot trn m?t bi?u m?u Windows.

Trong Microsoft Visual Basic 6.0, cch duy nh?t ? hi?n th? m?t h?nh ?nh t? m?t c s? d? li?u trong m?t PictureBox ki?m sot, m khng c b?c trung gian c?a ti?t ki?m nh? phn l?n ?i t?ng (BLOB) d? li?u vo m?t t?p tin, l ? rng bu?c cc PictureBox ? m?t ngu?n d? li?u nh l m?t d? li?u ActiveX Data Objects (ADO) ki?m sot ho?c RecordSet ?i t?ng. N?u khng c d? li?u rng bu?c, b?n khng th? l?p tr?nh n?p m?t BLOB m?t ki?m sot m khng lu h?nh ?nh vo m?t t?p tin cho cc LoadPicture tuyn b? ? s? d?ng.

Cc m?u trong bi vi?t ny s? d?ng m?t Byte m?ng v cc MemoryStream ?i t?ng t? cc System.io l?p c s? ? sao chp d? li?u ?nh t? c s? d? li?u tr?c ti?p t? m?t ADO.NET S? li?u ho?c DataReader ?i t?ng ? cc PictureBox ki?m sot.

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, h? t?ng m?ng, v gi d?ch v? ?c yu c?u:
  • Microsoft Visual Studio.NET ?c ci ?t trn m?t tng thch H? i?u hnh Microsoft Windows
  • Th? hi?n c s?n c?a Microsoft SQL Server hay m?t c s?n Microsoft Access c s? d? li?u ? th? nghi?m
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i sau cc ch? ?:
  • Visual Basic.Cc ?ng d?ng NET Windows Forms
  • ADO.Truy c?p d? li?u NET
  • Nh? phn ?i t?ng l?n (BLOB) lu tr? trong c s? d? li?u

T?o Visual Basic.NET m?u

  1. T?o ra m?t b?ng SQL Server hay m?t truy c?p b?ng v?i cc c?u trc sau y:
    CREATE TABLE BLOBTest
    (
    BLOBID INT IDENTITY NOT NULL,
    BLOBData IMAGE NOT NULL
    )
    					
  2. Lm theo cc b?c sau ? t?o ra m?t c?a s? m?i Visual Basic Cc h?nh th?c ?ng d?ng:
    1. B?t ?u Visual Studio.NET.
    2. Trn cc Tp tr?nh n, i?m ?n M?i, sau b?m D an.
    3. Trong cc D? n m?i h?p tho?i h?p, b?m vo Visual Basic d? n d?i Cc lo?i d? n, sau b?m ?ng d?ng Windows d?i Khun mu.
  3. Ko m?t PictureBox ki?m sot v hai Nt i?u khi?n t? h?p cng c? m?c ?nh m?u, Form1. Thay ?i cc Tn ti s?n c?a cc PictureBox ? picBlob. ?t cc Vn b?n ti s?n c?a Button1 ? T?p tin c s? d? li?u, v sau thi?t l?p cc Vn b?n ti s?n c?a Button2 ? C s? d? li?u ? PictureBox.
  4. Thm d?i y Nh?p kh?u pht bi?u ? ?u cc m-un m? Form1:
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.IO
    Imports System.Drawing.Imaging
    					
  5. Thm b?n tuyn ngn sau y cho k?t n?i c s? d? li?u chu?i d?i y Cng c?ng l?p Form1, v sau s?a ?i cc k?t n?i nh l c?n thi?t cho chu?i c?a b?n mi tr?ng:
        Dim strCn As String = "Data Source=<server>;" & _
                "Initial Catalog=<database>;Integrated Security=SSPI"
    					
  6. Thm m? sau trong cc Nh?p vo th? t?c t? ch?c s? ki?n Button1, v sau thay ?i cc ?ng d?n c?a t?p vo m?t t?p tin h?nh ?nh c s?n m?u khi c?n thi?t:
            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()
    						
    M? ny s? d?ng m?t FileStream ?i t?ng ?c t?p tin h?nh ?nh t? ?a vo m?t Byte m?ng v sau s? d?ng m?t parameterized Ch? huy ?i t?ng ? chn d? li?u trong c s? d? li?u.
  7. Thm m? sau trong cc Nh?p vo th? t?c t? ch?c s? ki?n Button2:
            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
    						
    M? ny l?y hng t? cc BLOBTest b?ng trong c s? d? li?u vo m?t S? li?u ?i t?ng, b?n sao h?nh ?nh l h?u h?t g?n y ? thm vo m?t Byte m?ng v sau vo m?t MemoryStream ?i t?ng, v sau t?i cc MemoryStream vo cc H?nh ?nh ti s?n c?a cc PictureBox ki?m sot.
  8. Ch?y cc d? n.
  9. Nh?p vo T?p tin c s? d? li?u ? t?i t m?t trong nh?ng m?u h?nh thnh c s? d? li?u.
  10. Nh?p vo C s? d? li?u ? PictureBox ? hi?n th? h?nh ?nh m b?n ? lu trong cc PictureBox ki?m sot.
  11. Truy xu?t h?nh ?nh trong m?t DataReader ?i t?ng thay v? m?t S? li?u ?i t?ng, thay ?i m? trong cc Nh?p vo th? t?c t? ch?c s? ki?n Button2 nh sau:
            Dim cn As New SqlConnection(strCn)
            Dim cmd As New SqlCommand("SELECT BLOBID, " & _
                "BLOBData FROM BLOBTest ORDER BY BLOBID", cn)
            Dim dr As SqlDataReader
            cn.Open()
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            If dr.Read Then
                Dim bytBLOBData(dr.GetBytes(1, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
                dr.GetBytes(1, 0, bytBLOBData, 0, bytBLOBData.Length)
                Dim stmBLOBData As New MemoryStream(bytBLOBData)
                picBLOB.Image = Image.FromStream(stmBLOBData)
            End If
            dr.Close()
    					
  12. ? chn h?nh ?nh t? cc PictureBox ki?m sot tr?c ti?p vo c s? d? li?u, thm m?t ph?n ba Nt ki?m sot)Button3) ? h?nh th?c c?a b?n. ?t cc Vn b?n ti s?n c?a Button3 ? PictureBox c s? d? li?u, v sau thm cc sau khi m? trong cc Nh?p vo th? t?c t? ch?c s? ki?n Button3:
            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()
    						
    M? ny l?y cc d? li?u h?nh ?nh t? cc PictureBox ki?m sot trong m?t MemoryStream ?i t?ng, b?n sao cc MemoryStream vo m?t Byte m?ng, v sau s? d?ng m?t parameterized Ch? huy ?i t?ng ? c?u cc Byte m?ng c s? d? li?u.
  13. Ch?y cc d? n. Nh?p vo C s? d? li?u ? PictureBox ? hi?n th? m?t h?nh ?nh m b?n lu tr?c trong cc PictureBox ki?m sot.
  14. Nh?p vo PictureBox c s? d? li?u ? c?u cc h?nh ?nh t? cc PictureBox c s? d? li?u, v sau nh?p vo C s? d? li?u ? PictureBox m?t l?n n?a ? xc nh?n r?ng h?nh ?nh ?c lu m?t cch chnh xc.

Khc phuc s c

  • Th? nghi?m ny khng lm vi?c v?i c?t ?nh trong cc Nhn vin b?ng c?a m?u Northwind c s? d? li?u ?c bao g?m v?i Access v SQL Server. Bitmap h?nh ?nh ?c lu tr? trong c?t ?nh ?c gi v?i tiu ? thng tin m cc Visual Basic 6.0 OLE Kho ch?a i?u khi?n t?o ra.
  • ? s? d?ng m?t c s? d? li?u truy c?p ? ki?m tra m? ny, b?n ph?i t?o cc c?t trong b?ng truy c?p nh lo?i OLE ?i t?ng v sau s? d?ng cc System.Data.OleDb khng gian tn c?a Microsoft ph?n l?c 4,0 nh cung c?p thay v? cc System.Data.SqlClient khng gian tn.
  • B?n tiu th? nhi?u b? nh? trn cao n?u b?n l?y l?i t?t c? h?nh ?nh t? m?t b?ng vo c?a b?n S? li?u. Tiu th? t hn trn cao, ? l?i nh?ng h?nh ?nh trong c s? d? li?u c?a b?n, v L?y chng m?t lc m?t th?i gian khi b?n di chuy?n qua c?a b?n S? li?u. ? th?c hi?n vi?c ny, th?c hi?n m?t truy v?n parameterized ? l?y h?nh ?nh m b?n mu?n vo m?t DataReader, v sau t?i h?nh ?nh t? cc DataReader vo cc PictureBox.

THAM KH?O

? thm thng tin v? cch s? d?ng d? li?u BLOB v?i Visual Basic.NET, nh?p vo s? bi vi?t d?i y ? xem cc bi vi?t trong c s? ki?n th?c Microsoft:
308042 Lm th? no ? ?c v vi?t BLOB d? li?u b?ng cch s? d?ng ADO.NET v?i Visual Basic.NET
316887 Lm th? no ? ?c v vi?t m?t t?p tin ?n v i t? m?t BLOB c?t b?ng cch s? d?ng ADO.NET v Visual Basic.NET

Thu?c tnh

ID c?a bi: 321900 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
T? kha:
kbhowtomaster kbsqlclient kbsystemdata kbmt KB321900 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:321900

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com