Extracting binary file from sql database image field to disk

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U2NvdHQ=?=

    #1

    Extracting binary file from sql database image field to disk

    I am trying to extract a zip file in a database image field to disk. For some
    reason, the zip file is getting corrupted / truncated. I have code in ASP
    which extracts the zip file no problem, so i know it is not corrupted in the
    table. Any help would be appreciated.

    FileStream fs = new FileStream(sFil ePath + "TestFile.z ip",
    FileMode.Create New);
    BinaryWriter bw = new BinaryWriter(fs );
    byte[] buffer =
    Encoding.ASCII. GetBytes(DataRe ader[sField].ToString());
    bw.Write(buffer );
    bw.Close();
    fs.Close();

  • Jon Skeet [C# MVP]

    #2
    Re: Extracting binary file from sql database image field to disk

    Scott <Scott@discussi ons.microsoft.c omwrote:
    I am trying to extract a zip file in a database image field to disk. For some
    reason, the zip file is getting corrupted / truncated. I have code in ASP
    which extracts the zip file no problem, so i know it is not corrupted in the
    table. Any help would be appreciated.
    >
    FileStream fs = new FileStream(sFil ePath + "TestFile.z ip",
    FileMode.Create New);
    BinaryWriter bw = new BinaryWriter(fs );
    byte[] buffer =
    Encoding.ASCII. GetBytes(DataRe ader[sField].ToString());
    bw.Write(buffer );
    bw.Close();
    fs.Close();
    Don't convert the binary data to a string - that's a recipe for a
    disaster.

    If you look at DataReader[sField].GetType(), what does it say?

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Matt F

      #3
      Re: Extracting binary file from sql database image field to disk

      I use the binaryformatter class for this type of thing --- MSDN example for
      the binaryformatter class has a pretty good example.

      Take a look at:
      http://msdn2.microsoft.com/en-us/lib...formatter.aspx


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Extracting binary file from sql database image field to disk

        Matt F <mfielderREMOVE CAPS@nospam.nos pamwrote:
        I use the binaryformatter class for this type of thing --- MSDN example for
        the binaryformatter class has a pretty good example.
        It's a zip file - natural binary data to start with. Serializing it
        would add an extra layer for no good reason, *and* make it
        hard/impossible to use from other platforms.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • =?Utf-8?B?U2NvdHQ=?=

          #5
          Re: Extracting binary file from sql database image field to disk



          "Jon Skeet [C# MVP]" wrote:
          Scott <Scott@discussi ons.microsoft.c omwrote:
          I am trying to extract a zip file in a database image field to disk. For some
          reason, the zip file is getting corrupted / truncated. I have code in ASP
          which extracts the zip file no problem, so i know it is not corrupted in the
          table. Any help would be appreciated.

          FileStream fs = new FileStream(sFil ePath + "TestFile.z ip",
          FileMode.Create New);
          BinaryWriter bw = new BinaryWriter(fs );
          byte[] buffer =
          Encoding.ASCII. GetBytes(DataRe ader[sField].ToString());
          bw.Write(buffer );
          bw.Close();
          fs.Close();
          >
          Don't convert the binary data to a string - that's a recipe for a
          disaster.
          >
          If you look at DataReader[sField].GetType(), what does it say?
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too
          >
          The DataReader[sField].GetType() comes back as System.String

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Extracting binary file from sql database image field to disk

            Scott <Scott@discussi ons.microsoft.c omwrote:
            If you look at DataReader[sField].GetType(), what does it say?
            The DataReader[sField].GetType() comes back as System.String
            Okay, that's a bad start. What's the data type in the database, and how
            have you stored the zip file in there in the first place? Storing a
            binary block of data (such as a zip file) in a text field is a really
            bad move...

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            Working...