Hallo,
hier ein detailierter Artikel, wie man alle möglichen Dinge in ein Binaryfeld wandeln kann.
Tip am Rande: Abstrakte Klasse (IDB…) wo man alle Aufgaben soweit möglich erledigt und nur konkrete Klassen, wenn die betreffende DB WIRKLICH „anders“ ist.
Chris
http://www.software-developers-home.de/modules.php?o…
außerdem hier noch ein paar Zeilen, um z.B. Bilder zu sichern:
‚‘’
‚‘’ Schreibt ein Image in das Medium
‚‘’
Public Overrides Function WriteImage(ByRef pic As Image, ByRef path As String) As Boolean
Dim con As IDbConnection = BuildConnection()
Dim cmd As IDbCommand = BuildCommand()
Try
con.ConnectionString = _connectionString
con.Open()
cmd.CommandText = String.Format(„INSERT INTO blubtest (idpk, pic, path) values (’{0}’, ic, ath)“, Guid.NewGuid().ToString())
cmd.Parameters.Add(BuildParameter(TransImageToStream(pic), „pic“))
cmd.Parameters.Add(BuildParameter(path, „path“))
cmd.Connection = con
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
_lastFm = String.Format(„Message: {0}“ & vbNewLine & „Source: {1}“ & vbNewLine & „Stacktrace: {2}“, ex.Message, ex.Source, ex.StackTrace)
Return False
Finally
con.Close()
End Try
End Function
‚‘’
‚‘’ Liest ein Image aus dem Medium
‚‘’
Public Overrides Function ReadImage(ByRef index As Guid) As Image
Dim con As IDbConnection = BuildConnection()
Dim cmd As IDbCommand = BuildCommand()
Dim bytes() As Byte
Dim reader As IDataReader
Dim sql As String = String.Format(„SELECT pic FROM blubtest WHERE idpk = ‚{0}‘“, index)
Try
con.ConnectionString = _connectionString
cmd.Connection = con
cmd.CommandText = sql
cmd.Connection.Open()
reader = cmd.ExecuteReader()
While reader.Read()
bytes = reader.GetValue(0)
End While
Return TransStreamToImage(bytes)
Catch ex As Exception
_lastFm = String.Format(„Message: {0}“ & vbNewLine & „Source: {1}“ & vbNewLine & „Stacktrace: {2}“, ex.Message, ex.Source, ex.StackTrace)
Finally
con.Close()
End Try
End Function
‚‘’
‚‘’ Gibt eine Liste der Images zurück (Index / Path)
‚‘’
Public Overrides Function ListImages() As IDictionary
Dim con As IDbConnection = BuildConnection()
Dim cmd As IDbCommand = BuildCommand()
Dim reader As IDataReader
Dim picList As IDictionary = New Hashtable
Dim sql As String = „SELECT idpk, path from BlubTest order by idpk“
Try
con.ConnectionString = _connectionString
cmd.Connection = con
cmd.CommandText = sql
cmd.Connection.Open()
reader = cmd.ExecuteReader()
While reader.Read()
picList.Add(reader.GetString(1), reader.GetString(0))
End While
Return picList
Catch ex As Exception
_lastFm = String.Format(„Message: {0}“ & vbNewLine & „Source: {1}“ & vbNewLine & „Stacktrace: {2}“, ex.Message, ex.Source, ex.StackTrace)
Finally
con.Close()
End Try
End Function
‚‘’
‚‘’ Testet die Connection, TRUE = Erfolgreiche Verbindung
‚‘’
Public Overrides Function TestConnection() As Boolean
Dim con As IDbConnection = BuildConnection()
Try
con.ConnectionString = _connectionString
con.Open()
Return True
Catch ex As Exception
_lastFm = String.Format(„Message: {0}“ & vbNewLine & „Source: {1}“ & vbNewLine & „Stacktrace: {2}“, ex.Message, ex.Source, ex.StackTrace)
Return False
Finally
con.Close()
End Try
End Function
‚‘’
‚‘’ Gibt alle Bilder zurück
‚‘’
Public Overrides Function GetAllImages() As IDictionary
Dim con As IDbConnection = BuildConnection()
Dim cmd As IDbCommand = BuildCommand()
Dim reader As IDataReader
Dim picList As IDictionary = New Hashtable
Dim sql As String = „SELECT idpk, pic from BlubTest order by idpk“
Try
con.ConnectionString = _connectionString
cmd.Connection = con
cmd.CommandText = sql
cmd.Connection.Open()
reader = cmd.ExecuteReader()
While reader.Read()
Dim id As String = reader.GetString(0)
Dim bytes() As Byte = reader.GetValue(1)
picList.Add(id, TransStreamToImage(bytes))
End While
Return picList
Catch ex As Exception
_lastFm = String.Format(„Message: {0}“ & vbNewLine & „Source: {1}“ & vbNewLine & „Stacktrace: {2}“, ex.Message, ex.Source, ex.StackTrace)
Finally
con.Close()
End Try
End Function