Câu hỏi

25/05/2013 20:27
Mình muốn update xâu ký tự kiểu "String" vào trong Cơ sơ có kiểu dữ liệu "Image" ?
Mong các bạn chỉ giúp, thanks
bombeo113
25/05/2013 20:27
Danh sách câu trả lời (1)

Để 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ó.
Đâ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
Ngoài ra bạn nên tham khảo thêm
http://caulacbovb.com/forum/viewtopic.php?f=25&t=10362
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ó.
Đâ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
Ngoài ra bạn nên tham khảo thêm
http://caulacbovb.com/forum/viewtopic.php?f=25&t=10362
Trả lời câu hỏi
Câu hỏi lĩnh vực Lập trình
Rao vặt Siêu Vip