Кто-нибудь использовал классический ASP с SQL Server 2008 и его новой функцией FileStream?

Я готов использовать возможности FileStream SQL Server 2008, но я не уверен, сможет ли Classic ASP читать и писать из FileStream SQL 2008. У нас все еще есть старое приложение, которое мы хотели бы обновить, чтобы оно поддерживало загрузку файлов в базу данных, и хотели бы рассмотреть FileStream. При необходимости я могу создать COM-объект через .NET, чтобы справиться с этим, но хотел бы знать, есть ли лучший способ, спасибо!


person tekiegreg    schedule 19.11.2009    source источник


Ответы (1)


Вот пример кода. Я не пробовал, но получил от http://www.experts-exchange.com/Microsoft/Development/MS_Access/Access_Coding-Macros/Q_24772719.html:

Function Download_File(SQL_Txt As String, Conn As ADODB.Connection, FieldName As String, Fname As String, Optional FPath As String = "") As String
        Dim Z As Variant, I As Long, Fn As String, Max As Long, X As New ADODB.Recordset, Fx As New FileSystemObject
        Dim FieldType As ADODB.DataTypeEnum
        Const Delta = 32768
        On Error GoTo Download_File_Err
        I = 1
        Download_File = ""


        X.CursorLocation = adUseServer
        X.Open SQL_Txt, Conn, adOpenStatic, adLockReadOnly

        Max = X(FieldName).ActualSize
        FieldType = X(FieldName).Type
        If FieldType = adLongVarChar Then
            Z = X(FieldName).GetChunk(Delta) 'Legggi la porzione di file...
        Else
            'FieldType =adLongVarBinary ->sicuramente!
            Z = BinaryToString(X(FieldName).GetChunk(Delta))
        End If
        'Apri-Crea il nuovo file e scrivi la prima porzione di file...
        Fx.OpenTextFile(Fn, ForWriting, True).Write Z
        While Len(Z) > 0
            If FieldType = adLongVarChar Then
                Z = Nz(X(FieldName).GetChunk(Delta), "")
            Else
                Z = BinaryToString(X(FieldName).GetChunk(Delta))
            End If
            'Salva la porzione di file...
            Fx.OpenTextFile(Fn, ForAppending, False).Write Z
            I = I + 1
        Wend
        X.Close
        Set Fx = Nothing
        Msg
        Download_File = Fn 'Segnala avvenuto scaricamento del file con nome e percorso...
        Exit Function
        Download_File_Err:
            MsgBox Err.Description
            Msg
            X.Close
        Set Fx = Nothing
End Function

Public Function Upload_File(SQL_Txt, Conn As ADODB.Connection, FieldName As String, Optional FPath_and_Name As String = "") As String
        Dim Z As String, L As Long, Fx As New FileSystemObject, X As New ADODB.Recordset
        Dim FieldType As ADODB.DataTypeEnum
        Const Delta = 16384
        On Error GoTo Upload_File_err
        Upload_File = ""
        L = FileLen(FPath_and_Name)
        'In questo caso si usa il cursore lato server... (Ma perchè il cursore lato client fallisce?)
        X.CursorLocation = adUseServer
        X.Open SQL_Txt, Conn, adOpenDynamic, adLockOptimistic
        FieldType = X(FieldName).Type
        If FieldType = adLongVarChar Then
            'Leggi tutti i caratteri dal file...
            Z = Fx.OpenTextFile(FPath_and_Name, ForReading).Read(L)
            X.Update FieldName, Z
        Else
            Z = Fx.OpenTextFile(FPath_and_Name, ForReading).Read(L)
            X.Update FieldName, StringToBinary(Z)
        End If
        X.Close
        Msg
        Upload_File = FPath_and_Name
        Exit Function
        Upload_File_err:
            MsgBox Err.Description
            X.Close
            Msg
End Function

Function BinaryToString(ByteArray As Variant) As String
        '--- Fast Converts the binary content to text
        'Antonin Foller, http://www.motobit.com
        Dim X As New ADODB.Recordset, L As Long
        BinaryToString = ""
        If IsNull(ByteArray) Then Exit Function
        L = LenB(ByteArray)
        If L > 0 Then
            X.Fields.Append "mBinary", adLongVarChar, L
            X.Open
            X.AddNew
            'In questo caso particolare AppendChunk converte l'array di byte
            'in stringa! fantastico.
            X("mBinary").AppendChunk ByteArray
            X.Update
            BinaryToString = X("mBinary")
        End If
        X.Close
End Function

Function StringToBinary(S As String) As Variant
        'Converts the string into a Binary array()
        'Standard conversion...
        Dim I As Long, V As Variant
        StringToBinary = Null
        If S = "" Then Exit Function
        ReDim V(0 To Len(S) - 1) As Byte
        For I = 1 To Len(S)
            V(I - 1) = Asc(Mid(S, I, 1))
        Next I
        StringToBinary = V
End Function
person D'Arcy Rittich    schedule 19.11.2009
comment
+1 за поиск, но вам нужно попробовать, прежде чем я назову это ответом - person tekiegreg; 19.11.2009