Probleme mit Oracle - Blobs unter C#



  • moin

    weis nicht genau ob ich hier richtig bin in dem unterforum aber da ich derzeit unter c# programmiere hab ich das mal hier hin gepackt:

    also mein problem:
    ich möchte ein bild in einer oracle datenbank abspeichern danach wieder auslesen und unter anderem namen abspeichern...

    dafür hab ich folgendes geschrieben:

    code zum reinschreiben in die datei:

    private void buttonSaveBlob_Click(object sender, EventArgs e)
            {
                LoadFile();
    
                int id = 0; 
                DataTable dt = db_con.execQuery("Select count(*) as count from argus_blob", true);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                       id = Convert.ToInt32((decimal)dt.Rows[i]["count"])+1;
                    }
                }
    
                db_con.insertBlob(_imageData, "INSERT INTO argus_blob (id, blob_size, blob) values (" + id + ", " + _imageData.Length + ", 'AAA')");
            }
    
            private void LoadFile()
            {
                try
                {
                    FileDialog fileDlg = new OpenFileDialog();
    
                    fileDlg.InitialDirectory =
                    "E:\\ws\\c++\\Projekte\\AVS Shiva\\AVS Shiva";
    
                    // Filter image(.jpg, .bmp, .gif) files only
                    fileDlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
                    fileDlg.RestoreDirectory = true;
                    if (fileDlg.ShowDialog() == DialogResult.OK)
                    {
                        _strImageName = fileDlg.FileName;
                    }
                    fileDlg = null;
                }
                catch (System.ArgumentException ex)
                {
                    _strImageName = "";
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
    
                FileStream fs;
                if (_strImageName != "")
                {
                    fs = new FileStream(@_strImageName, FileMode.Open, FileAccess.Read);
                    _imageLength = (int)fs.Length;
                    _imageData = new byte[fs.Length];
                    fs.Read(_imageData, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                }
            }
    
           public bool insertBlob(byte[] Data, string sqlstr)
            {
                for (int i = 0; i < 20; i++)
                {
                    MessageBox.Show("" + i + ": " + Data[i]);
                }
    
                OracleCommand command = conn.CreateCommand();
                bool erg = false;
    
                //Eintrag erzeugen in dem Blob gespeichert wird
                execNoQuery(sqlstr, true);
    
                command.Transaction = command.Connection.BeginTransaction();
    
                command.CommandText = "SELECT * FROM argus_blob FOR UPDATE";
                OracleDataReader reader = command.ExecuteReader();
                using (reader)
                {
                    reader.Read();
                    OracleLob BLOB1 = reader.GetOracleLob(1);
                    BLOB1.Write(Data, 0, Data.Length);
                    BLOB1.Position = 0;
                    command.Transaction.Commit();
                }
    
                return erg;
            }
    

    code zum auslesen in andere datei:

    private void buttonLoadBlob_Click(object sender, EventArgs e)
            {
                db_con.readBlob();
            }
    
           public bool readBlob() 
            {
                string id="0";
                DataTable dt = execQuery("select id from argus_blob order by id", true);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        id = "" + (decimal)dt.Rows[i]["id"];
                    }
                }
    
                OracleCommand command = conn.CreateCommand();
                int actual = 0;
    
                command.CommandText = "SELECT * FROM argus_blob where id=" + id + "";
                OracleDataReader reader = command.ExecuteReader();
                using (reader)
                {
                    reader.Read();
    
                    OracleLob blob = reader.GetOracleLob(1);
                    int blob_size = reader.GetInt32(2);
    
                    //Example - Reading binary data (in chunks).
                    byte[] buffer = new byte[blob_size];
                    while ((actual = blob.Read(buffer, 0, buffer.Length)) > 0)
                    {
    
                        SaveFile(buffer);
                    }
                }
                return true;
            }
    
            string _strImageName;
            private int _imageLength;
            private byte[] _imageData;
            private void SaveFile(byte[] buffer)
            {
                try
                {
                    FileDialog fileDlg = new SaveFileDialog();
    
                    fileDlg.InitialDirectory =
                    "E:\\ws\\c++\\Projekte\\AVS Shiva\\AVS Shiva";
    
                    // Filter image(.jpg, .bmp, .gif) files only
                    fileDlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
                    fileDlg.RestoreDirectory = true;
                    if (fileDlg.ShowDialog() == DialogResult.OK)
                    {
                        _strImageName = fileDlg.FileName;
                    }
                    fileDlg = null;
                }
                catch (System.ArgumentException ex)
                {
                    _strImageName = "";
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
    
                FileStream fs;
                if (_strImageName != "")
                {
                    fs = new FileStream(@_strImageName, FileMode.OpenOrCreate, FileAccess.Write);
                    _imageLength = (int)buffer.Length;
                    fs.Write(buffer, 0, _imageLength);
                    fs.Close();
                }
            }
    

    Problem: ich bekomme eine datei in der nur bildgröße +1 (+1 kann ich mir nicht erklären) und in dem bild steht als inhalt nur null drin ...

    wieso ist das so ???? was mach ich falsch ???



  • moin

    so hab nochmal nachgeschaut ...
    also ich hab mein *größen - problem" gelöst das war nur weil ich die datei nicht neu erzeugt hab hat der das nur angehangen ...

    2. problem: daten nicht richtig:
    da hab ich jetzt folgendes fest gestellt:
    wenn ich die tabelle lösche kann ich genau 2 blobs abspeichern und auslesen mit dem quellcode der gepostet wurde ...

    sobald ich ein 3. blob abspeichern will (sind 800 byte also net wild an daten) wird nur ein 0A in den blob gespeichert ...

    wieso ist das so ???

    mfg LT



  • ok 2. problem auch gelöst ...
    lag dran das ich in der funktion insertBlob die falsche zeile selectiert hatte ... naja nu hab ich die id angegeben die ich auch rein schreibe nu geht es ...


Anmelden zum Antworten