IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MS SQL Server Discussion :

Insertion dans un champ image


Sujet :

MS SQL Server

  1. #1
    Futur Membre du Club
    Inscrit en
    Mai 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 4
    Par défaut Insertion dans un champ image
    Bonjour,

    je voudrais savoir comment insert-on un fichier en binaire dans une table a partir d'un fichier présent sur le disque en sql?

    Merci

  2. #2
    Bradarys
    Invité(e)
    Par défaut
    Essai un champs de type BLOB.

    Tu lis le fichier et tu l'ecris dans ce champs.
    Je l'ai pas essayé, mais sa doit marcher.
    :

  3. #3
    Futur Membre du Club
    Inscrit en
    Mai 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 4
    Par défaut
    Le champ est obligatoirement de type image je peux pas le changer. Et je sais pas comment on lit le fichier pour l'insérer.

  4. #4
    Bradarys
    Invité(e)
    Par défaut
    Voici un ptit exemple tiré de l'aide de ms sql2000 SP3.
    j'ai pas eu assez de patience pour le lire en entier mais je pense que sa doit t'aider.

    Managing Long Data Types
    Long data types include ntext, text, and image data types. ntext, text, and image data can be so large that they cannot be retrieved in one operation or fit into memory. If the long data can fit into memory, the Value property of the Field object can be used to retrieve all the data in one operation. If the long data is too large to fit into memory, the data must be retrieved or written in chunks. You can manipulate long data in chunks through the Field object or through the Parameter object.

    The Field object allows you to write and read long data through the Recordset object. The AppendChunk method of the Field object allows you to append data at the end of the current data when the query has already been executed. The GetChunk method allows you to read the data in chunks.

    With the Parameter object, there is no GetChunk method, and there is no Recordset object when you are dealing with long data at run time. With the Parameter object, long data is bound at run time and executed with the Command object.

    There are some restrictions for long data when using MSDASQL. If no server cursor is used, all long columns must be to the right of all nonlong columns. If there are multiple long columns, the long columns must be accessed in order (from left to right).

    This example shows how to use ADO with SQLOLEDB to read and write image data. The critical routines are the while loops that copy the long data (image) to a variable and write the variable to a record in chunks (using the GetChunk and AppendChunk methods).

    Before setting up the destination table in this example, make sure to run the sp_dboption stored procedure:

    EXEC sp_dboption 'pubs', 'Select into/bulkcopy', 'True'

    The destination table is a copy of the pub_info table in the pubs database. Create the table by running:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    USE pubs
    SELECT * INTO pub_info_x
       FROM pub_info
    GO
    The pub_info_x table is the destination table in which the long data will be inserted.

    The ADO code is:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    Public Sub AppendChunkX()
     
       Dim cn As ADODB.Connection
       Dim rstPubInfo As ADODB.Recordset
       Dim strCn As String
       Dim strPubID As String
       Dim strPRInfo As String
       Dim lngOffset As Long
       Dim lngLogoSize As Long
       Dim varLogo As Variant
       Dim varChunk As Variant
     
       Const conChunkSize = 100
     
       ' Open a connection.
       Set cn = New ADODB.Connection
       strCn = "Server=srv;Database=pubs;UID=sa;Pwd=;"
     
       cn.Provider = "sqloledb"
       cn.Open strCn
     
       'Open the pub_info_x table.
       Set rstPubInfo = New ADODB.Recordset
       rstPubInfo.CursorType = adOpenDynamic
       rstPubInfo.LockType = adLockOptimistic
       rstPubInfo.Open "pub_info_x", cn, , , adCmdTable
     
       'Prompt for a logo to copy.
       strMsg = "Available logos are : " & vbCr & vbCr
     
       Do While Not rstPubInfo.EOF
          strMsg = strMsg & rstPubInfo!pub_id & vbCr & _ 
            Left(rstPubInfo!pr_info,
             InStr(rstPubInfo!pr_info, ",") - 1) & vbCr & vbCr
          rstPubInfo.MoveNext
       Loop
     
       strMsg = strMsg & "Enter the ID of a logo to copy:"
       strPubID = InputBox(strMsg)
     
       ' Copy the logo to a variable in chunks.
       rstPubInfo.Filter = "pub_id = '" & strPubID & "'"
       lngLogoSize = rstPubInfo!logo.ActualSize
       Do While lngOffset < lngLogoSize
          varChunk = rstPubInfo!logo.GetChunk(conChunkSize)
          varLogo = varLogo & varChunk
          lngOffset = lngOffset + conChunkSize
       Loop
     
       ' Get data from the user.
       strPubID = Trim(InputBox("Enter a new pub ID:"))
       strPRInfo = Trim(InputBox("Enter descriptive text:"))
     
       ' Add a new record, copying the logo in chunks.
       rstPubInfo.AddNew
       rstPubInfo!pub_id = strPubID
       rstPubInfo!pr_info = strPRInfo
       lngOffset = 0   ' Reset offset.
     
       Do While lngOffset < lngLogoSize
          varChunk = LeftB(RightB(varLogo, lngLogoSize - _ 
            lngOffset),conChunkSize)
          rstPubInfo!logo.AppendChunk varChunk
          lngOffset = lngOffset + conChunkSize
       Loop
     
       rstPubInfo.Update
     
       ' Show the newly added data.
       MsgBox "New record: " & rstPubInfo!pub_id & vbCr & _ 
         "Description: " & rstPubInfo!pr_info & vbCr & _ 
         "Logo size: " & rstPubInfo!logo.ActualSize
     
       rstPubInfo.Close
       cn.Close
     
    End Sub
    TOUJOURS TOUJOURS

  5. #5
    Rédacteur
    Avatar de WOLO Laurent
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Mars 2003
    Messages
    2 741
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Congo-Brazzaville

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2003
    Messages : 2 741
    Par défaut
    D'une manière générale, l'insertion des images dans une base de données bien que déconseillée, n'est possible que depuis un outils clients comme ADO ou ADO.Net
    Sachez que vous ne pouvez pas le faire en tout MS-SQL server.
    Quel est votre language de programmation ?

    Découvrez la FAQ de MS SQL Server.
    La chance accorde ses faveurs aux esprits avertis !

  6. #6
    Futur Membre du Club
    Inscrit en
    Mai 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 4
    Par défaut
    Je voulias le faire en tout SQL mais sinon j'utilise du VBScript

  7. #7
    Rédacteur
    Avatar de WOLO Laurent
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Mars 2003
    Messages
    2 741
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Congo-Brazzaville

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2003
    Messages : 2 741
    Par défaut
    Alors, utilise le code ADO qui t'a été donné.
    N'oublie pas également de faire usage des objects parameter pour les performances !

    Découvrez la FAQ de MS SQL Server.
    La chance accorde ses faveurs aux esprits avertis !

  8. #8
    Futur Membre du Club
    Inscrit en
    Mai 2005
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 4
    Par défaut
    Ok merci je vais essayer

  9. #9
    Membre chevronné
    Avatar de matazz
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    471
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 471
    Par défaut
    Moi je l'ai fait en C++ (Avec ADO) si ça peut t'aider :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    //*******************************************************************
    // Met des données binaires dans un variant
    //*******************************************************************
    BOOL PutBinaryIntoVariant(COleVariant * ovData, BYTE * pBuf, long cBufLen)
    		{
         BOOL fRetVal = FALSE;
     
         VARIANT var;
         VariantInit(&var);  //Initialize our variant
     
         //Set the type to an array of unsigned chars (OLE SAFEARRAY)
         var.vt = VT_ARRAY | VT_UI1;
     
         //Set up the bounds structure
         SAFEARRAYBOUND  rgsabound[1];
     
         rgsabound[0].cElements = cBufLen;
         rgsabound[0].lLbound = 0;
     
         //Create an OLE SAFEARRAY
         var.parray = SafeArrayCreate(VT_UI1,1,rgsabound);
     
         if(var.parray != NULL)
    			 {
           void * pArrayData = NULL;
     
           //Get a safe pointer to the array
           SafeArrayAccessData(var.parray,&pArrayData);
     
           //Copy bitmap to it
           memcpy(pArrayData, pBuf, cBufLen);
     
           //Unlock the variant data
           SafeArrayUnaccessData(var.parray);
     
           *ovData = var;  // Create a COleVariant based on our variant
           VariantClear(&var);
           fRetVal = TRUE;
    			}
         return fRetVal;
    		}
     
    //Puis 
     
    //Où DBRst est un _RecordsetPtr	 et varBlob est un COleVariant remplis par PutBinaryIntoVariant...
     
    this->DBRst->Fields->GetItem(_bstr_t(Fld))->AppendChunk(varBlob);

  10. #10
    Rédacteur/Modérateur

    Avatar de Fabien Celaia
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Octobre 2002
    Messages
    4 227
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : Suisse

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2002
    Messages : 4 227
    Billets dans le blog
    25
    Par défaut
    Citation Envoyé par Bradarys
    Essai un champs de type BLOB.
    Le type Large Object Binary sous MS-SQL est IMAGE !
    Pas de type BLOB sous MS-SQL...
    Sr DBA Oracle / MS-SQL / MySQL / Postgresql / SAP-Sybase / Informix / DB2

    N'oublie pas de consulter mes articles, mon blog, les cours et les FAQ SGBD

    Attention : pas de réponse technique par MP : pensez aux autres, passez par les forums !

Discussions similaires

  1. envoyer champ activex dans un champ image
    Par law56100 dans le forum WinDev
    Réponses: 2
    Dernier message: 23/05/2008, 16h41
  2. Insert dans des champs INT avec des doubles quotes
    Par bannik dans le forum Requêtes
    Réponses: 3
    Dernier message: 22/04/2008, 17h44
  3. Insertion dans plusieurs champs
    Par TallyHo dans le forum SQL Procédural
    Réponses: 2
    Dernier message: 23/10/2007, 10h40
  4. Pb d'insertion dans un champ primaire avec auto_increment
    Par jiper6f dans le forum Langage SQL
    Réponses: 5
    Dernier message: 04/09/2007, 09h11
  5. Ouverture d'un fichier stockée dans un champ image
    Par sat478 dans le forum Access
    Réponses: 1
    Dernier message: 08/09/2006, 18h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo