dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlBlob Class
Members Example

In This Topic
    MySqlBlob Class
    In This Topic
    Represents a variable-length stream of binary data to be stored in or retrieved from a database.
    Syntax
    Remarks
    MySqlBlob is used to insert or retrieve BLOB fields in databases. In MySQL, blob fields are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. You can also use MySqlBlob object to access other column types such as TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT, but in this case MySqlText is much more suitable.
    Example

    In this example two functions are presented. First can upload a file onto a server, and second can retrieve a BLOB field and paste it into a file. Notice that two classes used here (BinaryWriter and BinaryReader) to provide more flexible stream manipulation are actually redundant for simple data transfer operations.

    public void DownloadBlob(MySqlConnection myConnection)
    {
      MySqlCommand myCommand = new MySqlCommand("SELECT * FROM Test.Pictures", myConnection);
      myConnection.Open();
      MySqlDataReader myReader = myCommand.ExecuteReader(System.Data.CommandBehavior.Default);
      try
      {
        while (myReader.Read())
        {
          MySqlBlob myBlob = myReader.GetMySqlBlob(myReader.GetOrdinal("Picture"));
          if(!myBlob.IsNull)
          {
            string FN = myReader.GetString(myReader.GetOrdinal("PicName"));
            FileStream fs = new FileStream("D:\\Tmp\\"+FN+".bmp", FileMode.Create);
            BinaryWriter w = new BinaryWriter(fs);
            w.Write(myBlob.Value);
            w.Close();
            fs.Close();
            Console.WriteLine(FN+" downloaded.");
          }
        }
      }
      finally
      {
        myReader.Close();
        myConnection.Close();
      }
    }                                                                                                                   
    
    public void UploadBlob(MySqlConnection myConnection)
    {
      FileStream fs = new FileStream("D:\\Tmp\\_Water.bmp", FileMode.Open, FileAccess.Read);
      BinaryReader r = new BinaryReader(fs);
      MySqlBlob myBlob = new MySqlBlob(r.ReadBytes((int)fs.Length));
      MySqlCommand myCommand = new MySqlCommand("INSERT INTO Test.Pictures (ID, PicName, Picture) VALUES(1,'Water',:Pictures)", myConnection);
      myCommand.Parameters.Add("Pictures",myBlob);
      myConnection.Open();
      try
      {
        Console.WriteLine(myCommand.ExecuteNonQuery()+" rows affected.");
      }
      finally
      {
        myConnection.Close();
        r.Close();
      }
    }
    Public Sub DownloadBlob(ByVal myConnection As MySqlConnection)
      Dim myCommand As New MySqlCommand("SELECT * FROM Test.Pictures", myConnection)
      myConnection.Open()
      Dim myReader As MySqlDataReader = myCommand.ExecuteReader(System.Data.CommandBehavior.Default)
      Try
        While myReader.Read()
          Dim myBlob As MySqlBlob = myReader.GetMySqlBlob(myReader.GetOrdinal("Picture"))
          If Not myBlob.IsNull Then
            Dim FN As String = myReader.GetString(myReader.GetOrdinal("PicName"))
            Dim fs As FileStream = New FileStream("D:\Tmp\" + FN + ".bmp", FileMode.Create)
            Dim w As BinaryWriter = New BinaryWriter(fs)
            w.Write(myBlob.Value)
            w.Close()
            fs.Close()
            Console.WriteLine(String.Concat(FN, " downloaded."))
          End If
        End While
      Finally
        myReader.Close()
        myConnection.Close()
      End Try
    End Sub
    
    Public Sub UploadBlob(ByVal myConnection As MySqlConnection)
      Dim fs As FileStream = New FileStream("D:\Tmp\_Water.bmp", FileMode.Open, FileAccess.Read)
      Dim r As BinaryReader = New BinaryReader(fs)
      Dim myBlob As MySqlBlob = New MySqlBlob(r.ReadBytes(Convert.ToInt32(fs.Length)))
      Dim myCommand As MySqlCommand = New MySqlCommand("INSERT INTO Test.Pictures (ID, PicName, Picture) VALUES(2,'Water',:Pictures)", myConnection)
      myCommand.Parameters.Add("Pictures", myBlob)
      myConnection.Open()
      Try
        Console.WriteLine(String.Concat(myCommand.ExecuteNonQuery(), " rows affected."))
      Finally
        myConnection.Close()
        r.Close()
      End Try
    End Sub
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.IO.Stream
             Devart.Data.MySql.MySqlBlob
                Devart.Data.MySql.MySqlText

    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also