dotConnect Universal Documentation
Devart.Data.Universal Namespace / UniBlob Class
Members Example

In This Topic
    UniBlob Class
    In This Topic
    Represents a variable-length stream of binary data to be stored in or retrieved from a database.
    Syntax
    'Declaration
     
    Public Class UniBlob 
       Implements System.IDisposable 
    public class UniBlob : System.IDisposable  
    Remarks
    UniBlob is used to insert or retrieve BLOB fields in databases.

    See article Working with BLOB Data for detailed information.

    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(UniConnection myConnection)
    {
      UniCommand myCommand = new UniCommand("SELECT * FROM Test.Pictures", myConnection);
      myConnection.Open();
      UniDataReader myReader = myCommand.ExecuteReader(System.Data.CommandBehavior.Default);
      try
      {
        while (myReader.Read())
        {
          UniBlob myBlob = myReader.GetUniBlob(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(UniConnection myConnection)
    {
      FileStream fs = new FileStream("D:\\Tmp\\_Water.bmp", FileMode.Open, FileAccess.Read);
      BinaryReader r = new BinaryReader(fs);
      UniBlob myBlob = new UniBlob(r.ReadBytes((int)fs.Length));
      UniCommand myCommand = new UniCommand("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 UniConnection)
      Dim myCommand As New UniCommand("SELECT * FROM Test.Pictures", myConnection)
      myConnection.Open()
      Dim myReader As UniDataReader = myCommand.ExecuteReader(System.Data.CommandBehavior.Default)
      Try
        While myReader.Read()
          Dim myBlob As UniBlob = myReader.GetUniBlob(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 UniConnection)
      Dim fs As FileStream = New FileStream("D:\Tmp\_Water.bmp", FileMode.Open, FileAccess.Read)
      Dim r As BinaryReader = New BinaryReader(fs)
      Dim myBlob As UniBlob = New UniBlob(r.ReadBytes(Convert.ToInt32(fs.Length)))
      Dim myCommand As UniCommand = New UniCommand("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
       Devart.Data.Universal.UniBlob

    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