代码之家  ›  专栏  ›  技术社区  ›  Sunscreen

Infopath附件字段;在SQL Server中提交

  •  0
  • Sunscreen  · 技术社区  · 14 年前

    提前谢谢你, 太阳

    1 回复  |  直到 14 年前
        1
  •  0
  •   Kevin Ross    14 年前

    您可以将文件存储在SQL server的VarBinary(Max)列中。这将存储你放在那里的东西,以便你以后可以取回它。如果您使用的是SQL server 2008,那么您还可以使用文件流功能,该功能非常强大,工作方式完全相同

    下面是access/VBA项目中的一些代码,这些代码将下载存储在SQL server中的文件并将其保存到磁盘

    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