c#读取blob文件的内容,并在将其转换为文件时 - 它是不可读的

时间:2016-09-16 11:21:26

标签: c# firebird

我已经制作了一个获取blob列内容的工具,然后保存了文件

让我困扰了将近三天的问题是,在我保存文件后 - 它是不可读的。我尝试打开文件时的例外是: '无法读取文件头' 如果文件是.tif格式,我必须提到最多 我将不胜感激任何帮助

FbCommand cmd = new FbCommand(String.Format("SELECT FIRST 1 ID, DOCID, FILENAME, FILESIZE, DATA FROM ORIGINALS WHERE ID > {0} ORDER BY ID", initialIndex), con);

var reader = cmd.ExecuteReader();
if (reader.HasRows)
{
    while (reader.Read())
    {
        //MessageBox.Show(reader.GetInt32(0).ToString());
        int docId = (int)reader["DOCID"];

        long newDocId = dictDocs[docId];
        initialIndex = (int)reader["ID"];
        string fileName = reader["FILENAME"].ToString();
        int size = (int)reader["FILESIZE"];
        byte[] data = (byte[])reader["DATA"];


        System.IO.FileStream fs =
           new System.IO.FileStream("D:" + fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        fs.Write(data, 0, data.Length);
        fs.Close();

        var Writer = new BinaryWriter(File.OpenWrite("D:" + fileName));

         Writer.Write(data);
         Writer.Flush();

    }
}

1 个答案:

答案 0 :(得分:2)

所有

之后
System.IO.FileStream fs =
           new System.IO.FileStream("D:" + fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        fs.Write(data, 0, data.Length);
        fs.Close();

是不必要的。您正在覆盖自己的文件。