|
Tipp 0033
|
Grafiken in Access-Datenbank speichern
|
|
|
Autor/Einsender: Datum: |
|
Bernhard Froschauer 14.03.2004 |
|
Entwicklungsumgebung: |
|
VB.Net 2002 |
Framework: |
|
1.1 |
|
|
Dieses Beispiel zeigt, wie man Bilder binär in einer Datenbank speichern und wieder auslesen kann.
Das Feld in der Access-Datenbank, das die Binärdaten aufnehmen soll, muss vom Datentyp LongBinary sein.
|
|
|
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class bilderspeichern
Inherits System.Windows.Forms.Form Vom Windows Form Designer generierter Code
Private Puffer() As Byte
Private Sub bilderspeichern_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
btn_save.Enabled = False
End Sub
Public Function bildanzeigen(ByVal strfile As String) As Image
Dim myfilelength As Int64
Dim fs As FileStream = New FileStream(strfile, FileMode.Open)
Try
myfilelength = fs.Length
ReDim Puffer(myfilelength - 1)
fs.Read(Puffer, 0, myfilelength)
Dim b As Bitmap = New Bitmap(New MemoryStream(Puffer))
Return b
Catch ex As Exception
Finally
fs.Close()
End Try
End Function
Public Function bildspeichern(ByVal ID As Int32) As Image
Dim insertsql As String = "INSERT INTO tbl_bilder" & _
" (BILD, length, id)" & _
" VALUES (@bild,@length,@id)"
Dim myconn As OleDbConnection
Try
myconn = New OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=bld.mdb;")
Dim insertcmd As New OleDbCommand(insertsql, myconn)
insertcmd.Parameters.Add( _
"@bild", OleDbType.Binary).Value = Puffer
insertcmd.Parameters.Add( _
"@length", OleDbType.Integer).Value = Puffer.Length
insertcmd.Parameters.Add( _
"@id", OleDbType.Integer).Value = ID
myconn.Open()
insertcmd.ExecuteNonQuery()
btn_save.Enabled = False
lbl_Meldung.Text = _
"Bild erfolgreich in der Datenbank gespeichert."
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
Finally
myconn.Close()
End Try
End Function
End Class
|
|
|
|
|
Windows-Version |
98/SE |
|
|
ME |
|
|
NT |
|
|
2000 |
|
|
XP |
|
|
Vista |
|
|
Win
7 |
|
|
|
|
Download (61,1 kB)
|
Downloads bisher: [ 3057 ]
|
|
|