-1

We are trying to store an executable(exe) file in SQL. We are getting no error either writing or reading. Just the file we stored is not working after downloading back.

This is how we store the file:

databaseFilePut(@"FilePath", con, dt.Rows[0].ItemArray[0].ToString(), "ASIL");

And this is the inside of the function:

public static void databaseFilePut(string varFilePath, SqlConnection con, string version, string OFSET )
        {
            byte[] file;
            using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream))
                {
                    file = reader.ReadBytes((int)stream.Length);
                }
            }
            using (var sqlWrite = new SqlCommand("UPDATE ERP_TOOL_UPDATE SET Version=@Version1, ExeDosyasi= @ExeDosyasi1, OFSET= @OFSET1", con))
            {
                sqlWrite.Parameters.AddWithValue("@Version1", (double.Parse(version) + 1).ToString());
                sqlWrite.Parameters.Add("@ExeDosyasi1", SqlDbType.VarBinary, file.Length).Value = file;
                sqlWrite.Parameters.AddWithValue("@OFSET1", "ASIL");
                sqlWrite.ExecuteNonQuery();
            }
        }

After saving to the database this is what the data like:

0x4D5A90000300000004000000FFFF0000B8000.... and goes on.

After reading we try to recreate the stored exe with this code:

SqlCommand com = new SqlCommand("Select ExeDosyasi From ERP_TOOL_UPDATE WHERE OFSET = 'ASIL' ", con);
            com.CommandType = CommandType.Text;
            SqlDataReader reader = com.ExecuteReader();
            reader.Read();

            byte[] blob;
            byte[] blob2;

            blob = (byte[])reader[0];

            blob2 = System.Text.Encoding.Default.GetBytes(System.Text.Encoding.Unicode.GetString(blob));

            using (var fs = new FileStream(@"C:\Users\Bilal\Desktop\ERPAnalizTool.exe", FileMode.Create, FileAccess.Write))

            {
                fs.Write(blob2, 0, blob2.Length);
                fs.Flush();
            }

We are not getting any errors, it saves the file. Just the problem is the file has a little bit smaller size. When we try to run, it doesn't run. Like it never was an exe before.

Any help would be appreciated. Thank you all.

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • Maybe it gets marked as unsafe? See https://answers.microsoft.com/en-us/windows/forum/all/this-file-is-potentially-unsafe-to-open-need/b4038008-de01-44cc-8759-38970488b47b. – DaggeJ Aug 12 '20 at 09:23
  • 4
    You are decoding the binary data as text but that's not what you want to do with an executable file. There's no text encoding on an executable. – Christoph Lütjen Aug 12 '20 at 09:27
  • @DaggeJ Thank you for your answer, I don't think that's the problem. It has lesser size than normal. I think it is about my encoding and decoding method. Can't really find anything – Muhammed Lutfu ODABASOGLU Aug 12 '20 at 09:32
  • @ChristophLütjen Thank you for your answer. So you mean there is no way to store executable on SQL server database? Can you help me about the solution? – Muhammed Lutfu ODABASOGLU Aug 12 '20 at 09:35

1 Answers1

1

Your problem is the following line:

blob2 = System.Text.Encoding.Default.GetBytes(System.Text.Encoding.Unicode.GetString(blob));

The blob variable contains the bytes you wrote to the database, which is the content of the file read in the databaseFilePut method. There is no reason at all to convert it to a Unicode string and then to the system default encoding (Windows-1252 on my system). The data is not a string, it is binary. The double conversion will simply produce a mangled byte sequence.

Simply write the blob variable to disk:

blob = (byte[])reader[0];
File.WriteAllBytes(@"C:\Users\Bilal\Desktop\ERPAnalizTool.exe", blob);
Corey
  • 15,524
  • 2
  • 35
  • 68