使用MS Access 2010将文件上载到SQL Server 2008数据库

时间:2011-03-29 11:36:56

标签: sql-server-2008 ms-access

我需要将文件(例如excel,word等文档)加载到SQL Server 2008,以便可以从网站查看/下载它们。我需要能够从访问数据库和网站加载和打开文件。

任何人都可以帮忙,谢谢你。

2 个答案:

答案 0 :(得分:1)

以下是我在一个旧项目中的表现。你可以去除进度条的东西和其他一些东西,但你明白了

Public Sub Upload_file_OLD(lMaterial_ID As Long, strFile_name As String)
'upload the file to the selected material ID.
Dim adStream As ADODB.Stream
Dim rst As ADODB.Recordset
On Error GoTo Error_trap

'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 "MCTS"
        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
Me.acxProg_bar.Value = 10
Me.Repaint
adStream.LoadFromFile strFile_name
Me.acxProg_bar.Value = 50
Me.Repaint

Set rst = New ADODB.Recordset
rst.Open "SELECT Material_FS,Material_file_name, Material_size FROM tblMaterials WHERE Material_ID=" & lMaterial_ID, dbCon, adOpenKeyset, adLockOptimistic
Me.acxProg_bar.Value = 60
Me.Repaint
Me.txtFile_size = adStream.Size
rst.Fields("Material_FS").Value = adStream.Read
rst.Fields("Material_file_name").Value = GetFileName(strFile_name)
rst.Fields("Material_size").Value = adStream.Size
Me.acxProg_bar.Value = 90
Me.Repaint
rst.Update
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 Upload_file, error description, " & Err.Description, vbCritical, "MCTS"

End Sub

答案 1 :(得分:0)

我无法使用上面的代码,但我确实让它工作了。 SQL Server blob字段是varbinary(max)。

上载:

Sub TestDocUpload()
Dim cmd As New ADODB.Command
Dim st As New ADODB.Stream

st.Type = adTypeBinary
st.Open
st.LoadFromFile "c:\temparea\18572.pdf"

With cmd
    .CommandText = "Insert into tbldocuments(docblob, doctype) values (?, ?)"
    .CommandType = adCmdText
    .Parameters.Append .CreateParameter("@P1", adLongVarBinary, adParamInput, st.Size, st.Read)
    .Parameters.Append .CreateParameter("@P2", adVarChar, adParamInput, 50, "CustPO")

End With
If cnlocal.State = 0 Then OpenNewLocalConnection
cmd.ActiveConnection = cnlocal
cmd.Execute

End Sub

提取:

Sub TestReadDoc()
Dim myblob() As Byte
Dim rs As New ADODB.Recordset

If cnlocal.State = 0 Then OpenNewLocalConnection
rs.Open "tblDocuments", cnlocal, adOpenForwardOnly, adLockReadOnly
rs.MoveFirst
myblob = rs!DocBlob
Open "c:\temparea\output.pdf" For Binary Access Write As 1
Put #1, , myblob
Close #1

End Sub