Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Writing BLOB Values to a Data Source


You can write a binary large object (BLOB) to a database as either binary or character data, depending on the type of field at your data source. To write a BLOB value to your database, issue the appropriate INSERT or UPDATE statement and pass the BLOB value as an input parameter. If your BLOB is stored as text, such as a SQL Server text field, you can pass the BLOB as a string parameter. If the BLOB is stored in binary format, such as a SQL Server image field, you can pass an array of type byte as a binary parameter.

The following code example adds employee information to the Employees table in the DEMODB database. A photo of the employee is read from a file and added to the Photo field in the table, which is an image field.

NoteNote

You need to using CREATE TABLE statement to create a table, the table name is Employees. The SQL statement:

create table Employees(photo BLOB, id int);

 

using CUBRID.Data.CUBRIDClient;
    
  namespace BLOBExample
  {
      class Program
      {
          static void Main(string[] args)
          {
              CUBRIDConnectionStringBuilder sb = new CUBRIDConnectionStringBuilder("localhost", "demodb", "public", "", "33000", true);
              using (CUBRIDConnection conn = new CUBRIDConnection(sb.GetConnectionString()))
              {
                  conn.Open();
    
                  string sql = "insert into Employees (Photo) values(?)";
                  using (CUBRIDCommand cmd = new CUBRIDCommand(sql, conn))
                  {
                      CUBRIDBlob Blob = new CUBRIDBlob(conn);
                      byte[] bytes;
                      BinaryReader _reader = new BinaryReader(File.Open(@".\John.jpg", FileMode.Open));
                      int length = (int)_reader.BaseStream.Length;
                      bytes = _reader.ReadBytes(length);
  
                      Blob.setBytes(1, bytes);
                      CUBRIDParameter param = new CUBRIDParameter();
                      param.ParameterName = "?";
                      param.CUBRIDDataType = CUBRIDDataType.CCI_U_TYPE_BLOB;
                      param.DbType = DbType.Binary;
                      param.Value = Blob;

                      cmd.Parameters.Add(param);
                      cmd.ExecuteNonQuery();
                  }
  
                  conn.Close();
              }
          }
      }
  }
comments powered by Disqus
Page info
viewed 789 times
translations en
Author
posted last year by
kevinyang
Contributors
updated last year by
View revisions
Share this article