[英]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.