2015-09-28 34 views
0

即時工作在項目做文件管理,我剛剛完成代碼上傳文件到我的數據庫(.mdf)表,但我不知道如何下載文件回來到我的文件夾(或其他任何地方)。 我看到人們從ASP.Net或OR Web數據庫下載,但我希望有人能告訴我如何從mdf數據庫下載它。如何從mdf數據庫中下載文件c#

private void find_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog dlg = new OpenFileDialog(); 
    if (dlg.ShowDialog() == DialogResult.OK) 
    { 
    string Path = dlg.FileName.ToString(); 
    PathBox.Text = Path; 
    PicBox.location = Path; 
    } 
} 

private void save_Click(object sender, eventargs e) 
{ 
    byte[] Doc = null; 
    Filestream fs = new FileStream(this.PathBox.Text, FileMode.Open, FileAccess.Read); 
    BinaryReader br = new BinaryReader(fs); 
    Doc = br.ReadBytes((int)fs.Length); 

    string DS = "datasource = localDB ......"; 
    string Insertcmd = "insert into ......"; 
    MySqlConnection conn = new MySqlConnection(DS); 
    MySqlCommand cmd = new MySqlCommand(Insertcmd, conn); 
    MySqlDataReader msdr; 

    try 
    { 
     conn.Open(); 
     cmd.Parameter.Add(new MySqlParameter("@Document", Doc)); 

     msdr = cmd.ExecuteReader(); 
     MessageBox.Show("file saved"); 
     While (msdr.Read()) 
     { 
     } 
    catch (Exception ex) 
    { 
    } 
} 

有人可以給我一個代碼,可以從.mdf數據庫下載嗎?甚至任何暗示將是非常有益

感謝

+0

從數據庫中選擇文件並將字節數組寫入磁盤,如下所示:http://stackoverflow.com/questions/381508/can-a-byte-array-be-written-to-a-file- in-c搜索'將字節數組寫入磁盤'。 – GrandMasterFlush

+0

@GrandMasterFlush非常感謝你,這真的有很大的幫助:) – UnseenRoad

+0

@GrandMasterFlush嗨,我有一個問題,你可以請檢查此鏈接:http://stackoverflow.com/questions/32939079/value-cannot-be- null-parameter-name-bytes-suggestion-please – UnseenRoad

回答

0

HI看來,要保存在一個二進制格式(BLOB)對象的文件? 因此,當你想從數據庫中獲得圖像後顯示圖像時,你可以使用內存流讀取它並顯示它。 或者可能使用context.Response.BinaryWrite()來輸出圖像。

http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html可能是有幫助的。

+0

感謝您的回覆,我不確定是否將它保存爲BLOB - 仍在學習如何使用C#,但主要是針對PDF文檔和Word文檔,它們將包含文本和圖片。我會嘗試你的代碼,看看我是否可以輸出文件:p – UnseenRoad

相關問題