Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在C中从MemoryStream打开文件?_File_C# 4.0_Memorystream - Fatal编程技术网

File 如何在C中从MemoryStream打开文件?

File 如何在C中从MemoryStream打开文件?,file,c#-4.0,memorystream,File,C# 4.0,Memorystream,我已将文件保存到数据库中。这些文件可以是Doc、PDF或Image类型。现在,我尝试使用以下代码从DataGridView单元格单击事件打开这些文件 private void dgvDocuments_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == dgvDocuments.Columns[0].Index) { int

我已将文件保存到数据库中。这些文件可以是Doc、PDF或Image类型。现在,我尝试使用以下代码从DataGridView单元格单击事件打开这些文件

    private void dgvDocuments_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dgvDocuments.Columns[0].Index)
        {
            int id =Convert.ToInt32(dgvDocuments.Rows[e.RowIndex].Cells[1].Value);
            string query = "SELECT Photo FROM [dbo].[tblHR_Emloyee_Documents] WHERE ID = " + id;
            Utility.Generate_Window_Control.databaseFileRead(query);
        }
    }

    public static MemoryStream databaseFileRead(string query)
   {
       MemoryStream memoryStream = new MemoryStream();
       using (var varConnection = new SqlConnection(Utility.Global_Connection.conn))
       using (var sqlQuery = new SqlCommand(query, varConnection))
       {
           varConnection.Open();
           using (var sqlQueryResult = sqlQuery.ExecuteReader())
               if (sqlQueryResult != null)
               {
                   sqlQueryResult.Read();
                   var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
                   sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
                   //using (var fs = new MemoryStream(memoryStream, FileMode.Create, FileAccess.Write)) {
                   memoryStream.Write(blob, 0, blob.Length);
                   //}
               }
       }
       return memoryStream;
   }

在调试期间,它不会显示任何错误。值是正确的。但它并没有打开这些文件。请指导我如何执行此操作?

您的DataGridView单元格\u单击代码在哪里?如何打开文件?@HamletHakobyan我已经更新了代码。我不知道如何从内存流中打开,为什么不将其保存在文件中然后打开?@Hamlet Hakobyan我明白了,感谢Sadd memoryStream.Position=0,因此它从一开始就再次开始读取。或者使用MemoryStreambyte[]构造函数。