Hur gör man för att lagra en bild i en sql server 7.0????? Om du använder ADO 2.5 eller senare kanske detta är ett alternativ:lagra en bild?
Jag vet att man väljer image o sen kan jag inte få in bilden????
Jag har databasen o vill lägga in den direkt där i, utan en massa kod. Går inte det??Sv: lagra en bild?
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim params As ADODB.Parameters
Dim param As ADODB.Parameter
Set con = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=ANVÄNDARNAMN;Password=LÖSENORD;Initial Catalog=DATABASNAMN;Data Source=SERVERNAMN"
con.Open
With cmd
Set .ActiveConnection = con
.CommandText = "PROCEDURNAMN"
.CommandType = adCmdStoredProc
Set params = .Parameters
End With
Dim FileLength As Long
Dim SourceFile As Long
Dim ImgBuff() As Byte
SourceFile = FreeFile
Open strImagePath For Binary As #SourceFile
FileLength = LOF(SourceFile)
ReDim ImgBuff(FileLength) As Byte
Get #SourceFile, , ImgBuff()
Close #SourceFile
Set param = cmd.CreateParameter("@ImageContent", adLongVarBinary, adParamInput, (FileLength + 1))
params.Append param
param.AppendChunk ImgBuff()
cmd.Execute , , adExecuteNoRecordsSv: lagra en bild?
Public Function FileToField(Filename As String, Field As ADODB.Field)
Dim mStream As ADODB.Stream
Set mStream = New ADODB.Stream
With mStream
.Type = adTypeBinary
.Open
.LoadFromFile Filename
Field.Value = .Read
.Close
End With
Set mStream = Nothing
End Function
Public Function FieldToFile(Field As ADODB.Field, Filename As String)
Dim mStream As ADODB.Stream
Set mStream = New ADODB.Stream
With mStream
.Type = adTypeBinary
.Open
.Write Field
.SaveToFile Filename, adSaveCreateOverWrite
.Close
End With
Set mStream = Nothing
End Function
Public Function GetPictureFromField(Field As ADODB.Field, Optional TempFileName As String = "~TempPicture.tmp") As StdPicture
FieldToFile Field, TempFileName
Set GetPictureFromField = LoadPicture(TempFileName)
Kill TempFileName
End Function
Exempel på hur funktionerna används:
'Spara bild till recordset
Dim rsTemp As ADODB.Recordset
Set rsTemp = DataEnvironment1.rsPictures
rsTemp.Open "Tabell/Vy", "ConnectionString", adOpenKeyset, adLockOptimistic
rsTemp.AddNew
FileToField "C:\Bild.bmp", rsTemp("PictureData")
rsTemp.Update
rsTemp.Close
Set rsTemp = Nothing
'Hämta bild från recordset
Dim rsTemp As ADODB.Recordset
Set rsTemp = DataEnvironment1.rsPictures
rsTemp.Open , , adOpenStatic, adLockReadOnly
Set Image1.Picture = GetPictureFromField(rsTemp("PictureData"))
rsTemp.Close
Set rsTemp = Nothing