Search code examples
c#sql-serverfilefile-storagesqlfilestream

Stored exe File in sql is not executable after Loaded From SQL Server


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.


Solution

  • 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);