在MySql数据库中获取blob文件

时间:2013-12-30 08:57:55

标签: c# mysql

我正在尝试从MySql获取Blob(winWord doc。存储在MySql中)文件。一切正常,但当我尝试打开它时,MSWORD告诉我该文件的内容有问题。这是我的代码:

myConn.Open();
            MySqlDataReader myReader;
            long CurrentIndex = 0;
            long BytesReturned;

            using (myReader = view.ExecuteReader())
            {
                while (myReader.Read())
                {
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string strFilename = saveFileDialog1.FileName;
                        FileStream fs = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        BinaryWriter bw = new BinaryWriter(fs);
                        CurrentIndex = 0;
                        long len = 100;
                        byte[] blob = new byte[len];
                        int id = myReader.GetOrdinal("word");
                        BytesReturned = myReader.GetBytes(id, CurrentIndex, blob, 0, (int)len);

                        while (BytesReturned == (int)len)
                        {
                            bw.Write(blob);
                            bw.Flush();
                            CurrentIndex += (int)len;
                            BytesReturned = myReader.GetBytes(id, CurrentIndex, blob, 0, (int)len);


                        }
                        bw.Write(blob, 0, (int)len - 1);
                        bw.Flush();
                        bw.Close();
                        fs.Close();

                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

提前致谢。

2 个答案:

答案 0 :(得分:0)

以下文档建议您在数据库中存储和检索文件

http://dev.mysql.com/doc/refman/5.5/en/connector-net-programming-blob.html

答案 1 :(得分:0)

这个算法运行得很好,但代码中有一点错误。

字符串

bw.Write(blob, 0, (int)len - 1);

在while语句之后,需要替换为

bw.Write(blob, 0, BytesReturned);

结果代码(将blob保存到文件单独的方法,并将一些“using”用于FileStream和BinaryReader):

    public bool ReadDBBlobToFile ( MySqlDataReader parReader, string parFilePath, string parColumnName )
    {
        bool retResult = false;
        if ( parReader == null )
        {
            throw new NullReferenceException ( "MySqlCommand is null" );
        }

        int id = parReader.GetOrdinal(parColumnName);
        if ( !parReader.IsDBNull ( id ) )
        {
            string dir = Path.GetDirectoryName(parFilePath);
            if ( string.IsNullOrWhiteSpace ( dir ) )
            {
                dir = Path.GetDirectoryName ( Path.GetFullPath ( parFilePath ) );
            }
            Directory.CreateDirectory ( dir );

            using ( FileStream fs = new FileStream ( parFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite ) )
            {
                using ( BinaryWriter bw = new BinaryWriter ( fs ) )
                {
                    long CurrentIndex = 0;
                    long len = 100;
                    byte[] blob = new byte[len];

                    long BytesReturned = parReader.GetBytes ( id, CurrentIndex, blob, 0, ( int ) len );

                    while ( BytesReturned == len )
                    {
                        bw.Write ( blob );
                        bw.Flush ( );
                        CurrentIndex += len;
                        BytesReturned = parReader.GetBytes ( id, CurrentIndex, blob, 0, ( int ) len );
                    }
                    if ( BytesReturned > 0 )
                    {
                        bw.Write ( blob, 0, ( int ) BytesReturned );
                    }
                    bw.Flush ( );
                    bw.Close ( );
                }
                fs.Close ( );
            }
            retResult = true;
        }
        else
        {
            retResult = false;
        }
        return retResult;
    }


    myConn.Open();
        using (MySqlDataReader myReader = view.ExecuteReader())
        {
            while (myReader.Read())
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string strFilename = saveFileDialog1.FileName;
                    ReadDBBlobToFile ( myReader, strFilename, "word" );
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }