View Full Version : [Q] xin chỉ cách insert dữ liệu kiểu image!
Có ai chỉ mình cách insert dữ liệu kiểu image vào trong table với!.mình đã thử rồi nhưng khi kết nối với database thì không hiển thị được hình ảnh(có lẽ do insert sai).xin các bạn chỉ giáo chi tiết cho ng mới nhập môn với!!!.xin đa tạ.!!
tue_fithou86
28-10-2007, 21:01
Nếu bạn muốn lưu image hay picture bạn hãy xác định datatype là : BINARY
Chúc thành công
minhdien
29-10-2007, 15:50
Để lưu data dạng ảnh (bất kỳ tập tin dạng nào: bmp, jpg, doc,xls, ...) vào trong table thì :
1. trong table bạn phải khai báo một field có kiểu image,
2. viết code chuyển file cần lưu thành dạng binary, sau đó mới insert dữ liệu binary này vào field có kiểu image trong table.
Khi cần xem lại file này từ table trong database thì bạn phải làm bước ngược lại: chuyển từ dạng binary trong field image thành file tương ứng trên disk, rồi sau đó mới dùng phần mềm tương ứng (Word, Excel,Paint, ... )để xem nó.
duyvu667
08-11-2007, 10:24
các bác có thể chỉ em cách cụ thể hơn một được không, kiềm thêm ví dụ chẳng hạn !!! thanks .
Đây là 2 thủ tục viết bằng VB của mình: Lưu và lấy dữ liệu ảnh từ DB SQL Server
Private Sub SavePictureToDB(sFileName As String)
On Error GoTo Err_Save_Picture
Dim oPict As StdPicture
Dim RS As New ADODB.Recordset
Dim strStream As ADODB.Stream
Set oPict = LoadPicture(sFileName)
If oPict Is Nothing Then
MsgBox "Lçi file ¶nh.", vbOKOnly, "Th«ng b¸o"
Exit Sub
End If
If RS.State = 1 Then RS.Close
RS.Open "select LOGO from " & glbDatabaseName & ".DBO.THONG_TIN_DV", glbCONNECTION, adOpenKeyset, adLockOptimistic
If RS.EOF Then
RS.AddNew
End If
Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile sFileName
RS.Fields("LOGO").value = strStream.Read
strStream.Close
RS.Update
RS.Close
Exit Sub
Err_Save_Picture:
Process_Error Me.Name, "SavePictureToDB "
End Sub
Public Sub LoadPictureFromDB(picTMP As Image)
On Error GoTo Err_Load_Picrture
Dim RS As New ADODB.Recordset
Dim strStream As ADODB.Stream
If RS.State = 1 Then RS.Close
RS.Open "select LOGO from " & glbDatabaseName & ".DBO.THONG_TIN_DV", glbCONNECTION, adOpenDynamic, adLockBatchOptimistic
If RS.EOF Then
RS.Close
Exit Sub
End If
If RS Is Nothing Then
Exit Sub
End If
If Not IsNull(RS.Fields("LOGO").value) Then
Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.Write RS.Fields("LOGO").value
strStream.SaveToFile glbCurrentPath & "temppic.bmp", adSaveCreateOverWrite
picTMP.Picture = LoadPicture(glbCurrentPath & "temppic.bmp")
Kill glbCurrentPath & "temppic.bmp"
End If
RS.Close
Exit Sub
Err_Load_Picrture:
Process_Error "mdlFunctions", "LoadPictureFromDB"
End Sub
thanhlv84
08-11-2007, 20:28
tham khảo link này,dùng vb.net
http://caulacbovb.com/forum/viewtopic.php?f=25&t=10362
crykhohieu
15-10-2010, 22:59
Để lưu data dạng ảnh (bất kỳ tập tin dạng nào: bmp, jpg, doc,xls, ...) vào trong table thì :
1. trong table bạn phải khai báo một field có kiểu image,
2. viết code chuyển file cần lưu thành dạng binary, sau đó mới insert dữ liệu binary này vào field có kiểu image trong table.
Khi cần xem lại file này từ table trong database thì bạn phải làm bước ngược lại: chuyển từ dạng binary trong field image thành file tương ứng trên disk, rồi sau đó mới dùng phần mềm tương ứng (Word, Excel,Paint, ... )để xem nó.
bạn ơi, thế lúc tạo csdl mình để nó ở dạng binary lun đc ko?
voanhtu1988
19-10-2010, 15:16
Lúc lưu image nên lưu ở 1 mục nào đó, tránh lưu trực tiếp kiểu binary. trong db field image nên chỉ để nvarchar, lúc lưu thì lưu tên image thoy.
Mún xuất ra thì dùng
<img src="<dg dan toi image>/<tên image>">
vd: <img src="images/vidu.jpb" > /* vidu.jpb là tên hình lấy từ DB */
GL
Powered by vBulletin® Version 4.2.0 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.