2015-08-19 28 views
0

我試過從互聯網上的代碼,它工作正常。它可以將任何類型的文件上傳到SQL數據庫,並可以從SQL數據庫中檢索它。如何使用C#從sql數據庫中檢索任何類型的文件

但我的問題是我怎麼能打開任何形式的文件從SQL數據庫而不保存在計算機中。我想打開存儲的文件而不保存。就好像它是一個excel文件,我想在excel中打開它。然後用戶可以保存或不保存。謝謝你這是我的代碼..

private void button6_Click(object sender, EventArgs e) 
{ 
    SaveAttachment(sfdMain, gridViewMain); 
    FillDataGrid(gridViewMain, strQuery_AllAttachments); // refresh grid 
} 

private void SaveAttachment(SaveFileDialog objSfd, DataGridView objGrid) 
{ 
    string strId = objGrid.SelectedRows[0].Cells["ID"].Value.ToString(); 
    if (!string.IsNullOrEmpty(strId)) 
    { 
     SqlCommand sqlCmd = new SqlCommand(strQuery_GetAttachmentById, objConn); 
     sqlCmd.Parameters.AddWithValue("@attachId", strId); 
     SqlDataAdapter objAdapter = new SqlDataAdapter(sqlCmd); 
     DataTable objTable = new DataTable(); 
     DataRow objRow; 
     objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 
     SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(objAdapter); 
     objAdapter.Fill(objTable); 
     objRow = objTable.Rows[0]; 

     byte[] objData; 
     objData = (byte[])objRow["attachment"]; 

     if (objSfd.ShowDialog() != DialogResult.Cancel) 
     { 
      string strFileToSave = objSfd.FileName; 
      FileStream objFileStream = 
       new FileStream(strFileToSave, FileMode.Create, FileAccess.Read); 
      objFileStream.Write(objData, 0, objData.Length); 
      objFileStream.Close(); 
     } 
    } 
} 

private void button5_Click(object sender, EventArgs e) 
{ 
    if (ofdMain.ShowDialog() != DialogResult.Cancel) 
    { 
     CreateAttachment(ofdMain.FileName); //upload the attachment 
    } 
    FillDataGrid(gridViewMain, strQuery_AllAttachments); //refresh grid 
} 

private void CreateAttachment(string strFile) 
{ 
    SqlDataAdapter objAdapter = 
     new SqlDataAdapter(strQuery_AllAttachments_AllFields, objConn); 
    objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 
    SqlCommandBuilder objCmdBuilder = new SqlCommandBuilder(objAdapter); 
    DataTable objTable = new DataTable(); 
    FileStream objFileStream = 
     new FileStream(strFile, FileMode.Open, FileAccess.Read); 
    int intLength = Convert.ToInt32(objFileStream.Length); 
    byte[] objData; 
    objData = new byte[intLength]; 
    DataRow objRow; 
    string[] strPath = strFile.Split(Convert.ToChar(@"\")); 
    objAdapter.Fill(objTable); 

    objFileStream.Read(objData, 0, intLength); 
    objFileStream.Close(); 

    objRow = objTable.NewRow(); 
    //clip the full path - we just want last part! 
    objRow["fileName"] = strPath[strPath.Length - 1]; 
    objRow["fileSize"] = intLength/1024; // KB instead of bytes 
    objRow["attachment"] = objData; //our file 
    objTable.Rows.Add(objRow); //add our new record 
    objAdapter.Update(objTable); 
} 
+0

什麼限制你從簡單地將文件保存到一個臨時文件夾,將它傳遞給shell,以便它被適當的應用程序打開,監視進程,然後重新上傳文件,如果有任何修改? 雖然可以將其保存到內存流中,並在您的應用中與其進行交互,但其他應用程序(例如Excel)並非旨在這樣做。或者只是看看其他電子郵件應用程序如Microsoft自己的Outlook如何執行此操作,只需將該文件轉儲到臨時文件夾中,將其標記爲「已下載的文件」,並允許您編輯/保存到其他文件夾。 – Martheen

+0

你能幫我解決你的問題嗎 – Nidu

+0

你已經能夠保存文件了嗎?只需查找如何[將其保存到臨時文件](http://stackoverflow.com/questions/16656/creating-temporary-folders)。如果你感覺很慷慨,你可以[發送]到它的外殼(http://stackoverflow.com/questions/811521/how-do-i-open-a-file-using-the-shells-default-handler)所以用戶將打開適當的應用程序。至於下一步(監控文件並重新上傳它們),請跳過它(大多數電子郵件應用程序不提供此類功能),否則您要創建雲存儲應用程序,請使用現有的OneDrive/Google Drive API – Martheen

回答

1

就像一個警告,你可能會走錯了路線。將文件放入數據庫中很少有辦法。它可能會導致您的問題,而不僅僅是規模,但速度和數據庫負載。

相反,您爲什麼不調查雲存儲的衆多選項之一?例如,Azure具有可以使用的私有Blob存儲,並且它是爲此目的而構建的。它也有一個模擬器,所以你可以在本地進行測試。

+0

至於這個應用程序的請求,我需要這樣做。所以請幫助 – Nidu

相關問題