簡體   English   中英

從 SQL 服務器加載后,sql 中存儲的 exe 文件不可執行

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

我們正在嘗試在 SQL 中存儲一個可執行(exe)文件。 無論是寫入還是讀取,我們都沒有收到錯誤。 只是我們存儲的文件在下載回來后不起作用。

這是我們存儲文件的方式:

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

這是 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();
            }
        }

保存到數據庫后,數據如下:

0x4D5A90000300000004000000FFFF0000B8000....然后繼續。

閱讀后,我們嘗試使用以下代碼重新創建存儲的 exe:

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();
            }

我們沒有收到任何錯誤,它保存了文件。 只是問題是文件的大小有點小。 當我們嘗試運行時,它不會運行。 就像它以前從來不是一個exe一樣。

任何幫助,將不勝感激。 謝謝你們。

您的問題是以下行:

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

blob變量包含您寫入數據庫的字節,即在databaseFilePut方法中讀取的文件內容。 完全沒有理由將其轉換為 Unicode 字符串,然后轉換為系統默認編碼(我的系統上的 Windows-1252)。 數據不是字符串,它是二進制的。 雙重轉換只會產生一個錯位的字節序列。

只需將blob變量寫入磁盤:

blob = (byte[])reader[0];
File.WriteAllBytes(@"C:\Users\Bilal\Desktop\ERPAnalizTool.exe", blob);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM