如何从C#中的MySQL blob中检索zip文件?

时间:2017-06-18 09:35:24

标签: c# mysql

我在C#编程时遇到了问题。

有一个带有longBlob类型列的MySQL表。 LongBlob列包含zip文件。 我试着从那里检索zip文件, 但检索到的文件不是有效的zip文件。

MySQL正在开发linux系统, 我想在Windows系统上检索zip文件。

我的代码如下。哪里错了? 请帮我。 提前谢谢。

using (MySqlConnection conn = new MySqlConnection(connString))
        {
            conn.Open();

            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;

            MySqlDataReader reader = new MySqlDataReader();
            reader = cmd.ExecuteReader();
            reader.Read();

            byte[] data = (byte[])reader["ZipFileColumn"];
            File.WriteAllBytes(@"D:\test\test.zip", data);
            /* Zip file wasn't valid. */

            byte[] data2 = (byte[])reader["textFileColumn"];
            File.WriteAllBytes(@"D:\test\test.txt", data2);
            /* In case of text file, it was OK. */

        }

0 个答案:

没有答案