> Mas saya menggunakan VBNet, Database Access ada field Gambar OLE
OBject,
> Gimana cara nge binding field Gambar tersebut dalam picturebox ya.
::Kalo di binding langsung kayaknya tidak bisa deh, saya baca di
MSDN untuk menginputkan data image ke database anda bisa pake ADO,
filestream dan binarystream. contoh programnya spt ini:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class EmployeeData
Public Shared Sub Main()
Dim hireDate As DateTime = DateTime.Parse("5/21/99")
AddEmployee("Jones", "Mary", "Sales Representative", hireDate,
5, "jones.bmp")
End Sub
Public Shared Sub AddEmployee(lastName As String, firstName As
String, title As String, hireDate As DateTime, _
reportsTo As Integer, photoFilePath
As String)
Dim photo() as Byte = GetPhoto(photoFilePath)
Dim nwindConn As SqlConnection = New SqlConnection("Data
Source=localhost;Integrated Security=SSPI;Initial
Catalog=Northwind;")
Dim addEmp As SqlCommand = New SqlCommand("INSERT INTO Employees
(LastName, FirstName, Title, HireDate, ReportsTo, Photo) " & _
"Values(@LastName,
@FirstName, @Title, @HireDate, @ReportsTo, @Photo)", nwindConn)
addEmp.Parameters.Add("@LastName", SqlDbType.NVarChar,
20).Value = lastName
addEmp.Parameters.Add("@FirstName", SqlDbType.NVarChar,
10).Value = firstName
addEmp.Parameters.Add("@Title", SqlDbType.NVarChar,
30).Value = title
addEmp.Parameters.Add("@HireDate",
SqlDbType.DateTime).Value = hireDate
addEmp.Parameters.Add("@ReportsTo",
SqlDbType.Int).Value = reportsTo
addEmp.Parameters.Add("@Photo", SqlDbType.Image,
photo.Length).Value = photo
nwindConn.Open()
addEmp.ExecuteNonQuery()
nwindConn.Close()
End Sub
Public Shared Function GetPhoto(filePath As String) As Byte()
Dim fs As FileStream = new FileStream(filePath, FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = new BinaryReader(fs)
Dim photo() As Byte = br.ReadBytes(fs.Length)
br.Close()
fs.Close()
Return photo
End Function
End Class
Untuk mengambil image dari database maka logikanya dibaca dulu byte
per byte, kemudian dari byte2 tersebut disimpan menjadi sebuah file
temporary, terahir file tersebut di load ke picturebox.
Contoh programnya spt ini:
Dim pubsConn As SqlConnection = New SqlConnection("Data
Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;")
Dim logoCMD As SqlCommand = New SqlCommand("SELECT pub_id, logo FROM
pub_info", pubsConn)
Dim fs As FileStream ' Writes the BLOB to a file
(*.bmp).
Dim bw As BinaryWriter ' Streams the binary data to
the FileStream object.
Dim bufferSize As Integer = 100 ' The size of the BLOB buffer.
Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte() buffer to be
filled by GetBytes.
Dim retval As Long ' The bytes returned from
GetBytes.
Dim startIndex As Long = 0 ' The starting position in the
BLOB output.
Dim pub_id As String = "" ' The publisher id to use in
the file name.
' Open the connection and read data into the DataReader.
pubsConn.Open()
Dim myReader As SqlDataReader = logoCMD.ExecuteReader
(CommandBehavior.SequentialAccess)
Do While myReader.Read()
' Get the publisher id, which must occur before getting the logo.
pub_id = myReader.GetString(0)
' Create a file to hold the output.
fs = New FileStream("logo" & pub_id & ".bmp",
FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
' Reset the starting byte for a new BLOB.
startIndex = 0
' Read bytes into outbyte() and retain the number of bytes
returned.
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
' Continue reading and writing while there are bytes beyond the
size of the buffer.
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
' Reposition the start index to the end of the last buffer and
fill the buffer.
startIndex += bufferSize
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
Loop
' Write the remaining buffer.
bw.Write(outbyte, 0 , retval - 1)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
Loop
' Close the reader and the connection.
myReader.Close()
pubsConn.Close()
Me.PictureBox1.Image = LoadImage(0, "logo" & pub_id & ".bmp", 1, 0,
0, 16)
Notes:
Contoh code ini menggunakan SQL server, kalo anda menggunakan MS
access anda tinggal mengganti sqlClient menjadi OLEDB.
best regard
eko heri
Untuk berhenti berlangganan kirim email kosong ke : [EMAIL PROTECTED]
Ikuti juga forum diskusi VB.net dengan
mengirim email kosong ke [EMAIL PROTECTED]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/indoprog-vb/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/