save BLOB to file from MySQL using c#

Discussion in 'ASP .Net' started by Markusek Peter, Feb 19, 2004.

  1. Hi,
    I'm using MySQLDriverCS. I've got no problem to store BLOB into database,
    but I can't get it back(save to file).
    Problem is with DataTable(returns string:( )
    My code:
    --
    DataTable dt = new MySQLSelectCommand(...; //select that row and column
    where is BLOB
    string dest = Server.MapPath("image.jpg");
    FileStream binFile = new
    FileStream(dest,FileMode.OpenOrCreate,FileAccess.Write);
    --
    Now I want to use - binFile.Write(buf,0,buf.Length);. But problem is that
    dt.Rows[0]["ColumnBLOB"] is System.String type, and I need byte[](for
    binFile.Write)
    When I use this function :
    --
    public byte[] Serialize(object o)
    {
    MemoryStream s = new MemoryStream();
    BinaryFormatter b = new BinaryFormatter();
    b.Serialize(s, o);
    if (s.Length > int.MaxValue)
    throw new ArgumentException("Serialized object is larger than can fit into
    byte array");
    byte[] buffer = new Byte[s.Length];
    s.Seek(0, SeekOrigin.Begin);
    s.Read(buffer, 0, (int)s.Length);
    s.Close();
    return buffer;
    }
    --
    BLOB is saved into the file with the right size, but most of the characters
    are '?'(because
    dt.Rows[0]["ColumnBLOB"] is string?)

    =====================
    I'm sorry for bad english.
    Sample code will be appreciated:)
    Thanks.
     
    Markusek Peter, Feb 19, 2004
    #1
    1. Advertising

  2. Done: save BLOB to file from MySQL using c#

    I did it with ByteFX drivers, because MySQLDriverCS has not defined
    methods(like MySQLDataReader.GetBytes(...) )

    Here is sample code-
    --
    string connectionString =

    "Server=localhost;" +

    "Database=iso_dokumentacia;" +

    "User ID=;" +

    "Password=;";

    MySqlConnection cn = new MySqlConnection(connectionString);


    FileStream fs; // Writes the BLOB to a file (*.jpg).

    BinaryWriter bw; // Streams the BLOB to the FileStream object.

    int bufferSize = 100; // Size of the BLOB buffer.

    byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be
    filled by GetBytes.

    long retval; // The bytes returned from GetBytes.

    long startIndex = 0; // The starting position in the BLOB output.


    cn.Open();

    MySqlCommand logoCMD = new MySqlCommand("SELECT KapitolaBLOB FROM kapitola
    WHERE ID_KAPITOLA=15",cn);


    MySqlDataReader myReader =
    logoCMD.ExecuteReader(CommandBehavior.SequentialAccess);

    while (myReader.Read())

    {

    // Get the publisher id, which must occur before getting the logo.


    // Create a file to hold the output.

    string dest = Server.MapPath("s.jpg");

    fs = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write);

    bw = new BinaryWriter(fs);

    // Reset the starting byte for the new BLOB.

    startIndex = 0;

    // Read the bytes into outbyte[] and retain the number of bytes returned.

    //myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

    retval =(long) myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

    // Continue reading and writing while there are bytes beyond the size of the
    buffer.

    while (retval == bufferSize)

    {

    bw.Write(outbyte);

    bw.Flush();

    // Reposition the start index to the end of the last buffer and fill the
    buffer.

    startIndex += bufferSize;

    retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

    }

    // Write the remaining buffer.

    bw.Write(outbyte, 0, (int)retval - 1);

    bw.Flush();

    // Close the output file.

    bw.Close();

    fs.Close();

    }

    myReader.Close();

    cn.Close();

    }

    --
     
    Markusek Peter, Feb 19, 2004
    #2
    1. Advertising

  3. Markusek Peter

    chemacecilia

    Joined:
    Jun 9, 2007
    Messages:
    1
    How did you solved?

    Hello. I found this message looking for a possible solution for exactly the same problem that you showed up long time ago. So, please if you could solved and you can help me now. I would be grateful.
    Cheers
     
    chemacecilia, Jun 9, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moerderin

    Blob using MySQL

    Moerderin, Jul 30, 2004, in forum: Java
    Replies:
    6
    Views:
    10,005
    John Fereira
    Jul 31, 2004
  2. Big George
    Replies:
    0
    Views:
    410
    Big George
    Nov 20, 2006
  3. tomo
    Replies:
    2
    Views:
    1,093
  4. Pasha Aryana

    BLOB - classic ASP using MySql

    Pasha Aryana, Dec 27, 2007, in forum: ASP General
    Replies:
    2
    Views:
    230
    Anthony Jones
    Dec 29, 2007
  5. Bryan Richardson

    How to save Ruby object as a blob in MySQL

    Bryan Richardson, Sep 4, 2008, in forum: Ruby
    Replies:
    3
    Views:
    288
    Ezra Zygmuntowicz
    Sep 5, 2008
Loading...

Share This Page