2009-11-19 37 views
3

我准备使用SQL Server 2008的FileStream功能,但我不确定经典ASP是否可以从SQL 2008 FileStream读取和写入。我们仍然有一个旧应用程序,我们想更新以支持文件上传到数据库,并且想要考虑FileStream。如果需要,我可以通过.NET构建一个COM对象来处理这个问题,但想知道是否有更好的方法,谢谢!有没有人使用SQL Server 2008的经典ASP,它的新FileStream功能?

回答

2

这是一些示例代码。我没有尝试过,但从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 
+0

+1查看它, – tekiegreg

相关问题