Skip to content

UsageExamplesCSharpGetObject

Keith T. Garner edited this page Apr 4, 2014 · 1 revision
using System;
using System.IO;
using System.Collections;
using System.Data;
using System.Data.Odbc;

namespace EzRets
{
    class GetObjectTest
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                Hashtable extensions = new Hashtable();
                extensions["image/jpeg"] = "jpg";
                extensions["image/gif"] = "gif";

                OdbcConnection dbc = new OdbcConnection("DSN=retstest");
                dbc.Open();

                string query = "SELECT * from object:binary:Property where " +
                    "type='Photo' and object_key='LN000001'";
                OdbcCommand com = new OdbcCommand(query, dbc);

                OdbcDataReader dr = com.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string ext = (string) extensions[dr.GetString(2)];
                        string filename = "c:\\" + dr.GetString(0) + "." +
                            dr.GetInt32(1) + "." + ext;
                        Console.WriteLine("{0}", filename);

                        Stream outStream = File.OpenWrite(filename);

                        const int BUFFER_SIZE = 10000;
                        byte[] buf = new Byte[BUFFER_SIZE];
                        int read = 0;
                        int totalRead = 0;
                        while ((read = (int) dr.GetBytes(4, totalRead, buf, 0,
                                                         BUFFER_SIZE)) > 0)
                        {
                            outStream.Write(buf, 0, read);
                            Console.WriteLine("{0}", read);
                            totalRead = totalRead + read;
                            read = 0;
                        }
                        outStream.Close();
                    }
                }
                else
                {
                    Console.WriteLine("No rows returned.");
                }
                dr.Close();
                com.Dispose();
                dbc.Close();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Exception: {0}", e);
            }
        }
    }
}
Clone this wiki locally