6 Mayıs 2013 Pazartesi

Oracleda blob olarak kayıtlı resimleri topluca jpeg olarak diske yazma


//   Oracle DAC for NET kurulur
  //  Oracle.DataAccess referans olara  eklenir  ve  using Oracle.DataAccess.Client;  eklenir..

  private void button4_Click(object sender, EventArgs e)
        {
            /*
               http://www.codeproject.com/Articles/13365/Insert-retrieve-an-image-into-from-a-blob-field-in
             */

            string oraHost = "ORACLESUNUCU";
            string oraPort = "1521";
            string oraSid = "SERVISADI";
            string oraUsr = "KULLANICIADI";
            string oraPass = "ŞİFRE";
         

            label1.Text = "Bekleyiniz...";
            string OrclConStr="Data Source=TNSTEKISERVISADI;User iD=" + oraUsr + ";Password=" + oraPass;


            string pth = @"D:\RESIM_YOLU";


            string log = pth + "\\log.txt";
            File.WriteAllText(log, DateTime.Now.ToString() + " Oracledan Resim Güncelleme : " + Environment.NewLine);

            int say=0;

             try
            {

            OracleConnection con = new OracleConnection();
         
               
            con.ConnectionString = OrclConStr;
     
            DataSet ds = new DataSet();

               string sql = "select PERSID,FOTOGRAF from tblRESIMLER ";  // where rownum<10 ";
             
                OracleDataAdapter da = new OracleDataAdapter(sql, con);
             
                da.Fill(ds);
         

                DataTable dataTable = ds.Tables[0];

                FileStream FS;
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    string pid = dataRow[0].ToString();
                 
                 
                    string resimyol=pth + @"\" + pid + ".jpg";


                               
                    string txt =pid + " : Resim zaten var yazılmadı." +  Environment.NewLine;

                  if(!File.Exists(resimyol))
                  {
               
                        FS= new FileStream(resimyol, FileMode.Create);
                        byte[] blob = (byte[])dataRow[1];
                        FS.Write(blob, 0, blob.Length);
                     
                        FS.Close();
                        FS = null;

                        txt = pid + " : AKTARILDI." + Environment.NewLine;

                        say++;
                   }
                 
                   File.AppendAllText(log, txt);
             
                }
       
         
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


            label1.Text = "";
       
            MessageBox.Show(say.ToString() + " adet resim dosya olarak aktarıldı. Detay için Log.txt");

        }