2010-02-21 2 views
3

J'ai écrit une petite procédure VBA pour tester le téléchargement et le téléchargement de fichiers en tant que données binaires dans et hors d'une colonne VarBinary dans SQL Server utilisant ADO. Le processus de téléchargement semble fonctionner, mais je n'arrive pas à faire fonctionner le processus de téléchargement.Puis-je renvoyer un tableau d'octets à partir d'une colonne SQL Server VarBinary à l'aide d'une requête paramétrée?

Je crois que le paramètre de sortie pour VarBinary est mal configuré, mais je ne trouve aucune documentation sur la façon de le faire correctement.

J'ai une erreur d'exécution 3708 "L'objet de paramètre est mal défini, des informations incohérentes ou incomplètes ont été fournies." à la ligne .Parameters.Append .CreateParameter("@myblob", adVarBinary, adParamOutput)

Mise à jour: SELECT ? = myblob FROM bin_table WHERE ID = ?; semble revenir une chaîne binaire, pas un tableau binaire. Je crois que c'est là que réside le problème, mais je ne sais toujours pas comment le réparer.

Update: Je fixé le « incompatibilité de type: tableau ou type défini par l'utilisateur attendu » erreur de compilation en ajoutant ajoutant .Value à la fin de la ligne WriteFile "C:\some_new_file.pdf", .Parameters("@myblob").

Toute aide est grandement appréciée. Merci!

Private Sub TestReadWriteBlob() 

    Dim objConnection As New ADODB.Connection 
    Dim objCommand As New ADODB.Command 
    Dim objRecordset As New ADODB.Recordset 
    Dim intNewID As Integer 

    With objConnection 
     .CursorLocation = adUseClient 
     .ConnectionString = "PROVIDER=SQLOLEDB;Server=<server>;Database=<database>;UID=<uid>;PWD=<pwd>;trusted_connection=false;" 
     .Open 
    End With 

    With objCommand 
     .ActiveConnection = objConnection 
     .CommandText = "INSERT INTO bin_table (myblob) VALUES (?); SELECT ? = id FROM bin_table WHERE ID = @@IDENTITY;" 
     .CommandType = adCmdText 
     .Parameters.Append .CreateParameter("@myblob", adVarBinary, adParamInput, -1, ReadFile("C:\some_file.pdf")) 
     .Parameters.Append .CreateParameter("@NewID", adInteger, adParamOutput) 
     .Execute 
     intNewID = .Parameters("@NewID") 
    End With 

    Debug.Print intNewID 

    Set objCommand = Nothing 
    With objCommand 
     .ActiveConnection = objConnection 
     .CommandText = "SELECT ? = myblob FROM bin_table WHERE ID = ?;" 
     .CommandType = adCmdText 
     .Parameters.Append .CreateParameter("@myblob", adVarBinary, adParamOutput) 
     .Parameters.Append .CreateParameter("@NewID", adInteger, adParamInput, , intNewID) 
     .Execute 
     WriteFile "C:\some_new_file.pdf", .Parameters("@myblob").Value 
    End With 

End Sub 

Public Function ReadFile(ByVal strPath As String) As Byte() 

    Dim intFile As Integer 

    intFile = FreeFile 
    Open strPath For Binary Access Read As intFile 
    ReDim ReadFile(LOF(intFile) - 1) 
    Get intFile, , ReadFile 
    Close intFile 

End Function 

Public Sub WriteFile(ByVal strPath As String, bytBlob() As Byte, Optional ByVal Overwrite As Boolean = True) 

    Dim intFile As Integer 

    intFile = FreeFile 
    If Overwrite And Dir(strPath) <> "" Then 
     Kill strPath 
    End If 
    Open strPath For Binary Access Write As intFile 
    Put intFile, , bytBlob 
    Close intFile 

End Sub 

Répondre

2

Je n'ai trouvé aucun moyen de renvoyer le tableau d'octets de la colonne VarBinary dans SQL Server à l'aide d'un paramètre. J'ai, cependant, compris que le faire à partir du jeu d'enregistrements fonctionne. Le code joint fait le travail.

Je suis toujours à la recherche d'un moyen d'utiliser le paramètre pour renvoyer le tableau d'octets et résistera à accepter une réponse pendant quelques jours au cas où quelqu'un aurait une solution.

Private Sub TestReadWriteBlob() 

    Dim objConnection As New ADODB.Connection 
    Dim objCommand As New ADODB.Command 
    Dim intNewID As Integer 

    With objConnection 
     .CursorLocation = adUseClient 
     .ConnectionString = "PROVIDER=SQLOLEDB;Server=<server>;Database=<database>;UID=<uid>;PWD=<pwd>;trusted_connection=false;" 
     .Open 
    End With 

    With objCommand 
     .ActiveConnection = objConnection 
     .CommandText = "INSERT INTO bin_table (myblob) VALUES (?); SELECT ? = id FROM bin_table WHERE ID = @@IDENTITY;" 
     .CommandType = adCmdText 
     .Parameters.Append .CreateParameter("@myblob", adVarBinary, adParamInput, -1, ReadFile("C:\Users\Thomas\Desktop\some_file.pdf")) 
     .Parameters.Append .CreateParameter("@NewID", adInteger, adParamOutput) 
     .Execute 
     intNewID = .Parameters("@NewID") 
    End With 

    Set objCommand = Nothing 
    With objCommand 
     .ActiveConnection = objConnection 
     .CommandText = "SELECT myblob FROM bin_table WHERE ID = ?;" 
     .CommandType = adCmdText 
     .Parameters.Append .CreateParameter("@NewID", adInteger, adParamInput, , intNewID) 
     WriteFile "C:\Users\Thomas\Desktop\blob\some_file.pdf", .Execute.Fields("myblob").Value 
    End With 

End Sub 

Public Function ReadFile(ByVal strPath As String) As Byte() 

    Dim intFile As Integer 

    intFile = FreeFile 
    Open strPath For Binary Access Read As intFile 
    ReDim ReadFile(LOF(intFile) - 1) 
    Get intFile, , ReadFile 
    Close intFile 

End Function 

Public Sub WriteFile(ByVal strPath As String, bytBlob() As Byte, Optional ByVal Overwrite As Boolean = True) 

    Dim intFile As Integer 

    intFile = FreeFile 
    If Overwrite And Dir(strPath) <> "" Then 
     Kill strPath 
    End If 
    Open strPath For Binary Access Write As intFile 
    Put intFile, , bytBlob 
    Close intFile 

End Sub 
Questions connexes