Search code examples
c#mysqlblobblobsblobstorage

C# MySQL Blob field - Cannot copy zip file stored in blob to another table


i have a zip file stored in a table by a third party application which i have no control over. I do however have access to the MySQL DB.

What i want to do is do a SELECT statement to retrieve a blob field and copy this record into another table. But on the other side, i do see the blob field on the far side but it is not a zip file, its a text file that says System.Byte[] and thats it - anyone any ideas on what is causing this and how to fix it?

heres what i have below - again any help greatly appreciated :)

OdbcCommand broadcastSelect = new OdbcCommand("select * from exchange where status='1' and type='UPDATE'", cloud);
                OdbcDataReader DbReader = Select.ExecuteReader();
                int fCount = DbReader.FieldCount;
                String type = "";
                String filename = "";
                byte[] data = null;
                int status = 0;

                while (DbReader.Read())
                {
                    if (DbReader.IsDBNull(0))
                    {
                        type = "BLANK";
                    }
                    else
                    {
                        type = (string)DbReader[0];
                    }
                    if (DbReader.IsDBNull(1))
                    {
                        filename = "BLANK";
                    }
                    else
                    {
                        filename = (string)DbReader[1];
                    }
                    if (DbReader.IsDBNull(2))
                    {
                        data = new byte[1];
                    }
                    else
                    {
                        data = (byte[])DbReader[2];
                    }
                    if (DbReader.IsDBNull(3))
                    {
                        status = 0;
                    }
                    else
                    {
                        status = (int)DbReader[3];
                    }

                    OdbcCommand Copy = new OdbcCommand("INSERT INTO exchange(type,filename,data,status) VALUES('" + type + "','" + filename + "','"
                        + data + "','" + status + "')", local);
                    Copy.ExecuteNonQuery();

                }

Solution

  • use sql parameter for inserting the binary data.

    OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary); 
    

    ---Updated I hope below given code will be helpful to you.

     OdbcCommand broadcastSelect = new OdbcCommand("select * from exchange where status='1' and type='UPDATE'", cloud);
            OdbcDataReader DbReader = Select.ExecuteReader();
            int fCount = DbReader.FieldCount;
            String type = "";
            String filename = "";
            byte[] data = null;
            int status = 0;
            OdbcParameter param = null;
            while (DbReader.Read())
            {
                if (DbReader.IsDBNull(0))
                {
                    type = "BLANK";
                }
                else
                {
                    type = (string)DbReader[0];
                }
                if (DbReader.IsDBNull(1))
                {
                    filename = "BLANK";
                }
                else
                {
                    filename = (string)DbReader[1];
                }
                if (DbReader.IsDBNull(2))
                {
                    param = new OdbcParameter("@file", SqlDbType.Binary);
                    param.DbType = DbType.Binary;
                    param.Value = new byte[1];                
                    command.Parameters.Add(param); 
                }
                else
                {
                    param = new OdbcParameter("@file", SqlDbType.Binary);
                    param.DbType = DbType.Binary;
                    param.Value = (byte[])dbReader[2];
                    param.Size = ((byte[])dbReader[2]).Length; 
                    command.Parameters.Add(param); 
                }
                if (DbReader.IsDBNull(3))
                {
                    status = 0;
                }
                else
                {
                    status = (int)DbReader[3];
                }
    
                OdbcCommand Copy = new OdbcCommand("INSERT INTO exchange(type,filename,data,status) VALUES('" + type + "','" + filename + "',@file,'" + status + "')", local);
                Copy.ExecuteNonQuery();