PDA

View Full Version : ذخیره با filestream



h1and1saman
دوشنبه 03 آبان 1395, 00:17 صبح
سلام
در نسخه های جدید sql برای ذخیره عکس یا فایل از چیزی با عنوان filestream استفاده میشه .آیا میشه از این قابلیت در vb6 هم استفاده کرد .یعنی عکس یا فایل رو ذخیره کرد
در کار با sql های نسخه های جدید vb6 خداروشکر مشکلی نداره

m.4.r.m
پنج شنبه 13 آبان 1395, 19:02 عصر
'*Setup:
'*Create a form and place 3 command buttons named:
'*cmdLoad, cmdSelectSave, and cmdClear
'*Place a CommonDialog Control Named Dialog
'*Place an ImageBox (or PictureBox) named Image1




'** The field type in Sql Server must be "Image"
'** Everywhere you see "***" in the code is where you must enter
'** your own data.


Private Sub cmdClear_Click()
Image1.Picture = Nothing

End Sub


Private Sub cmdLoad_Click()
If Not LoadPictureFromDB(rstRecordset) Then
MsgBox "Invalid Data Or No Picture In DB"
End If
End Sub


Private Sub cmdSelectSave_Click()
'Open Dialog Box
With dlgDialog
.DialogTitle = "Open Image File...."
.Filter = "Image Files (*.gif; *.bmp)| *.gif;*.bmp"
.CancelError = True
procReOpen:
.ShowOpen

If .FileName = "" Then
MsgBox "Invalid filename or file not found.", _
vbOKOnly + vbExclamation, "Oops!"
GoTo procReOpen
Else
If Not SavePictureToDB(rstRecordset, .FileName) Then
MsgBox "Save was unsuccessful :(", vbOKOnly + _
vbExclamation, "Oops!"
Exit Sub
End If
End If

End With
End Sub


Private Sub Form_Load()
Set cnnConnection = New ADODB.Connection
Set rstRecordset = New ADODB.Recordset


cnnConnection.Open ("Provider=SQLOLEDB; " & _
"data Source=**YourServer**;" & _
"Initial Catalog=**YourDatabase**; " & _
"User Id=**YourUID**;Password=***YourPass***")
rstRecordset.Open "Select * from YourTable", cnnConnection, _
adOpenKeyset, adLockOptimistic



End Sub




Public Function LoadPictureFromDB(RS As ADODB.Recordset)


On Error GoTo procNoPicture

'If Recordset is Empty, Then Exit
If RS Is Nothing Then
GoTo procNoPicture
End If

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open

strStream.Write RS.Fields("**YourImageField**").Value



strStream.SaveToFile "C:\Temp.bmp", adSaveCreateOverWrite
Image1.Picture = LoadPicture("C:\Temp.bmp")
Kill ("C:\Temp.bmp")
LoadPictureFromDB = True


procExitFunction:
Exit Function
procNoPicture:
LoadPictureFromDB = False
GoTo procExitFunction
End Function


Public Function SavePictureToDB(RS As ADODB.Recordset, _
sFileName As String)


On Error GoTo procNoPicture
Dim oPict As StdPicture

Set oPict = LoadPicture(sFileName)

'Exit Function if this is NOT a picture file
If oPict Is Nothing Then
MsgBox "Invalid Picture File!", vbOKOnly, "Oops!"
SavePictureToDB = False
GoTo procExitSub
End If

RS.AddNew



Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile sFileName
RS.Fields("***YourImageField***").Value = strStream.Read

Image1.Picture = LoadPicture(sFileName)
SavePictureToDB = True




procExitSub:
Exit Function
procNoPicture:
SavePictureToDB = False
GoTo procExitSub
End Function

isaac23
شنبه 15 آبان 1395, 07:23 صبح
دوست عزیز اگه ممکنه سورس اینو بذارید خیلی لازم شده ممنونم

m.4.r.m
شنبه 15 آبان 1395, 21:08 عصر
خب مگه این سورس نیست ؟

می خوای پروژه بنویسم ؟ من حالشو ندارم کل سورس رو برات گذاشتم استفاده کن