2010-04-19 52 views

回答

0

可以存儲在VARBINARY(max)列在SQL服務器上的文件。這將存儲您放在那裏的所有內容,以便您稍後可以檢索它。如果您使用的是SQL Server 2008,您也可以使用功能非常強大的文件流功能,並且工作方式完全相同

以下是訪問/ VBA項目中的一些代碼,我將下載一個文件存儲在SQL服務器中並將其保存到磁盤

Public Sub Download_file(lMaterial_ID As Long, strSave_folder As String) 
'Download the file lMaterial_ID and save it 
Dim adStream As ADODB.Stream 
Dim rst As ADODB.Recordset 
On Error GoTo Error_trap 
On Error GoTo 0 
'check if we have an open connection, if we do use it 
Select Case dbCon.State 
    Case adStateOpen 
     'connection is open, do nothing 
    Case adStateConnecting 
     'still conecting wait 
     Do Until dbCon.State = adStateOpen 
      Application.Echo True, "Connection to DB" 
     Loop 
    Case adStateClosed 
     'connection closed, try to open it 
     If Len(strSQL_con_string) = 0 Then 
      Set_SQL_con 
     End If 
     dbCon.ConnectionString = strSQL_con_string 
     dbCon.Provider = "sqloledb" 
     dbCon.Open 
End Select 

Me.acxProg_bar.Value = 0 
Me.acxProg_bar.Visible = True 
Me.Repaint 

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


Set rst = New ADODB.Recordset 
rst.Open "SELECT Material_FS, Material_file_name FROM tblMaterials WITH (NOLOCK) WHERE Material_ID=" & lMaterial_ID, dbCon, adOpenForwardOnly, adLockReadOnly 
Me.acxProg_bar.Value = 60 
Me.Repaint 
If IsNull(rst.Fields("Material_FS").Value) = False Then 
    adStream.Write rst.Fields("Material_FS").Value 
    Me.acxProg_bar.Value = 80 
    Me.Repaint 
    adStream.SaveToFile strSave_folder & "\" & rst.Fields("Material_file_name").Value, adSaveCreateOverWrite 
End If 
rst.Close 
dbCon.Close 
Me.acxProg_bar.Value = 0 
Me.acxProg_bar.Visible = False 
Me.Repaint 


Exit Sub 

Error_trap: 

If dbCon Is Nothing = False Then 
    If dbCon.State = adStateOpen Then dbCon.Close 
End If 

DoCmd.Hourglass False 
MsgBox "An error happened in sub Download_file, error description, " & Err.Description, vbCritical, "MCTS" 
Me.acxProg_bar.Value = 0 
Me.acxProg_bar.Visible = False 
Me.Repaint 
End Sub