2012-07-17 76 views
6

我在读取MySQL数据库中的BLOB时遇到了一些麻烦。我已经成功地将它插入数据库,但似乎无法让它回读。我知道你们中的一些人可能会想“为什么他使用数据库来存储图像的斑点,而不仅仅是文件路径/文件名”,但我想要有灵活性,因为很多这些图像将存储在服务器而不是本地的,这样可以优化效率,并且允许我在需要时将图像移动到本地。我遵循了一个(简短的)教程,并且写下了以下用于接收blob的方法;读取MySQL数据库中的BLOB图像

public void getBlob(string query, string fileOut) 
    { 
     if (this.OpenConnection() == true) 
     { 
      MySqlCommand cmd = new MySqlCommand(query, mConnection); 

      //the index number to write bytes to 
      long CurrentIndex = 0; 

      //the number of bytes to store in the array 
      int BufferSize = 100; 

      //The Number of bytes returned from GetBytes() method 
      long BytesReturned; 

      //A byte array to hold the buffer 
      byte[] Blob = new byte[BufferSize]; 


      //We set the CommandBehavior to SequentialAccess 
      //so we can use the SqlDataReader.GerBytes() method. 

      MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); 

      while (reader.Read()) 
      { 
       FileStream fs = new FileStream(DeviceManager.picPath + "\\" + reader["siteBlobFileName"].ToString(), FileMode.OpenOrCreate, FileAccess.Write); 
       BinaryWriter writer = new BinaryWriter(fs); 
       CurrentIndex = 0; 
       BytesReturned = reader.GetBytes(1, CurrentIndex, Blob, 0,BufferSize); 

       while (BytesReturned == BufferSize) 
       { 
        writer.Write(Blob); 
        writer.Flush(); 
        CurrentIndex += BufferSize; 
        BytesReturned = reader.GetBytes(1, CurrentIndex, Blob, 0, BufferSize); 
       } 

       writer.Write(Blob, 0, (int)BytesReturned); 
       writer.Flush(); 
       writer.Close(); 
       fs.Close(); 
      } 
      reader.Close(); 

      this.CloseConnection(); 
     } 
    } 

和我很喜欢这样称它..

mDBConnector.getBlob("SELECT siteMapPicture, siteBlobFilename FROM sites WHERE siteID = '" + DeviceManager.lastSite + "'", DeviceManager.picPath + "mappicsite" + DeviceManager.lastSite); 


PBSite.BackgroundImage = Image.FromFile(DeviceManager.picPath + "mappicsite" + DeviceManager.lastSite); 

但是它示数在BytesReturned = reader.GetBytes(1,CURRENTINDEX,斑点,0,缓冲区大小);错误“GetBytes只能在二进制或guid列上调用”。我假设这是与我的数据库中的字段类型有关,但将列更改为键入二进制意味着我必须将其存储为blob类型,但我希望将文件名保留为常规字符串。有什么我失踪?或另一种方式做到这一点?

编辑1:我认为字节返回的第一个参数是在读取器中的列,将其设置为0会给出错误“无效尝试使用SequentialAccess读取先前列”,否则请仔细观察一下。

edit2:删除顺序访问给我一个大小为13字节的文件(这可能只是第一行,这就是为什么顺序访问读取所有行?),所以也许我按照错误的顺序读取列。

编辑3:我相信这个错误的原因是由于我输入数据库的方式。已经改变这个方法,我saveBlob现在看起来像这样:

public void saveBlob(string filePath, string fileName, string siteID) 
    { 
     if (this.OpenConnection() == true) 
     { 

      //A stream of bytes that represnts the binary file 
      FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); 

      //The reader reads the binary data from the file stream 
      BinaryReader reader = new BinaryReader(fs); 

      //Bytes from the binary reader stored in BlobValue array 
      byte[] BlobValue = reader.ReadBytes((int)fs.Length); 

      fs.Close(); 
      reader.Close(); 


      MySqlCommand cmd = new MySqlCommand(); 
      cmd.Connection = mConnection; 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "INSERT INTO x (y, z) VALUES (@BlobFile, @BlobFileName)"; 

      MySqlParameter BlobFileNameParam = new MySqlParameter("@BlobFileName", SqlDbType.NChar); 
      MySqlParameter BlobFileParam = new MySqlParameter("@BlobFile", SqlDbType.Binary); 
      cmd.Parameters.Add(BlobFileNameParam); 
      cmd.Parameters.Add(BlobFileParam); 
      BlobFileNameParam.Value = fileName; 
      BlobFileParam.Value = BlobValue; 



       cmd.ExecuteNonQuery(); 

      this.CloseConnection(); 
     } 
    } 

我已经通过调试运行,都blobvalue和blobfileparam(@blobfile)具有全尺寸(约15万),但它的示数在执行该查询,给出以下错误;

​​

我已经看到了入代码,并试图改变类型的二进制图像,允许更大的文件,但给出了同样的错误。任何人都对这个新信息有所了解?

编辑4:固定一切。注意到,在我的代码我使用:

("@BlobFile", SqlDbType.Binary); 

改变了这些类型的“MySQLDbType的”(DERP),它让我选择类型的斑点。事情终于按预期工作:)

+1

我不是C#专家,但你选择2列 - 'siteMapPicture'和'siteBlobFilename' - 都是斑点或者是一个VARCHAR /炭/文本? – 2012-07-17 15:54:07

+0

siteMapPicture是一个BLOB,siteBlobFilename是一个varchar – 2012-07-17 15:56:39

+0

不是c#开发人员,但不会是你的blob字段是列#0,而你想要在第1列getbybytes,这是路径字段? – 2012-07-17 16:39:17

回答