Unable to read BLOB objects through ADO.NET

G

Guest

Hi,

I need the help deperately.

My requirement is that i have to retrieve BLOB object from the Oracle and
need to save it on the Hard Disk through ADO.NET.

For this purpose i have tried it with OLEDB.NET Data provider and also
through the oracle client.

As i know that BLOB object contains Microsoft Word Document i am saving it
through Filestream object.

for your information i am writing the code:

********************************************************
using OLEDB.NET Data Provider
********************************************************

private void button1_Click(object sender, System.EventArgs e)
{
try
{
//string sConnString = "Provider=OraOLEDB.Oracle;Data
Source=FIT_USRISK.WORLD;User ID=fit_dba;Password=vintage2001";
string sConnString = "Provider=OraOLEDB.Oracle;Data
Source=AMS_TEST.WORLD;User ID=fit_dba;Password=ireland";
int PictureCol = 0; // the column # of the BLOB field
OleDbConnection cn = new OleDbConnection(sConnString);


OleDbCommand cmd = new OleDbCommand("SELECT " +
"OBJ " +
"FROM BIN " +
"WHERE BIN_ID = " + 10966, cn);

cn.Open();
OleDbDataReader dr =
cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
dr.Read();
Byte[] b = new Byte[(dr.GetBytes(PictureCol, 0, null, 0, int.MaxValue))];
dr.GetBytes(PictureCol, 0, b, 0, b.Length);
dr.Close();
cn.Close();
System.IO.FileStream fs =
new System.IO.FileStream("C:\\test25.doc", System.IO.FileMode.Create,
System.IO.FileAccess.Write);

fs.Write(b, 0, b.Length);
fs.Close();
MessageBox.Show("Image written to file successfully");
}
catch(OleDbException ex)
{
MessageBox.Show (ex.Message);
}

********************************************************
using OracleClent Data Provider
********************************************************
private void button1_Click(object sender, System.EventArgs e)
{
OracleConnection _conn=null;
// For fetching read only rows from datasource
OracleDataReader oraImgReader;

// For executing SQL statements against datasource
OracleCommand oraImgCmd;

try
{

// Replace with your datasource value (TNSNames)
";Data Source=" + "AMS_TEST.WORLD" ;

// Connection to datasource, using connection parameters given above
_conn = new OracleConnection(connectionString);

// Open database connection
_conn.Open();

}
// Catch exception when error in connecting to database occurs
catch (Exception ex)
{
// Display error message
MessageBox.Show(ex.ToString());

}
try
{


oraImgCmd = new OracleCommand(
"SELECT " +
"BIN_ID, " +
"OBJ " +
"FROM BIN " +
"WHERE BIN_ID = " + 10966 ,_conn);

// Set OracleConnection for this instance of OracleCommand
oraImgCmd.Connection = _conn;

// Set Command type as text
oraImgCmd.CommandType = CommandType.Text;

// Sends the CommandText to the Connection
// and builds an OracleDataReader
oraImgReader = oraImgCmd.ExecuteReader();

// Read data
// Returns true if another row exists; otherwise, returns false.
Boolean recordExist = oraImgReader.Read();

// If data exists
if (recordExist)
{
int BinID=0;
// Store current Employee value
if (!oraImgReader.IsDBNull(0))
{
BinID = Convert.ToInt32(oraImgReader["BIN_ID"]);
}


// If Photo exists in the Database, load it into the PictureBox
if (oraImgReader.GetValue(1).ToString() != "")
{

// Fetch the BLOB data through OracleDataReader using OracleBlob type
OracleLob blob = oraImgReader.GetOracleLob(1);
//Byte[] b=(byte[]) oraImgReader["OBJ"];
// Create a byte array of the size of the Blob obtained
Byte[] b = new Byte[blob.Length];

// Read blob data into byte array
int i = blob.Read(b,0,System.Convert.ToInt32(blob.Length));

System.IO.FileStream fs =
new System.IO.FileStream("C:\\test25.doc", System.IO.FileMode.Create,
System.IO.FileAccess.Write);

// System.IO.FileStream fs =
// new System.IO.FileStream("C:\\test21.doc",
System.IO.FileMode.Create);
fs.Write(b, 0, b.Length);
//fs.Write(b, 0, b.Length-1);
fs.Close();


// Get the primitive byte data into in-memory data stream

// System.IO.MemoryStream memStream = new System.IO.MemoryStream(b);
//
// // Attach the in-memory data stream to the PictureBox
// pictureBox1.Image = Image.FromStream(memStream);
//
// // Fit the image to the PictureBox size
// pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

MessageBox.Show("Image written to file successfully");
}
// close the OracleDataReader
oraImgReader.Close();
}

// Reset variable

}

// Catch exception when accessing arrary element out of bound
catch (System.IndexOutOfRangeException rangeException)
{
// Do nothing
rangeException.ToString();
}
catch (Exception ex)
{
// Display error message
System.Windows.Forms.MessageBox.Show( ex.ToString());
}
}



In both the above cases i am getting the doc document but i am unable to
open in Word 2000.It's giving the error that word is unable to open the
specified format.

please help as it is very urgent.
 
P

Paul Clement

On Sun, 6 Feb 2005 21:21:02 -0800, "(e-mail address removed)"

¤ Hi,
¤
¤ I need the help deperately.
¤
¤ My requirement is that i have to retrieve BLOB object from the Oracle and
¤ need to save it on the Hard Disk through ADO.NET.
¤
¤ For this purpose i have tried it with OLEDB.NET Data provider and also
¤ through the oracle client.
¤
¤ As i know that BLOB object contains Microsoft Word Document i am saving it
¤ through Filestream object.
¤

See if the following helps:

http://msdn.microsoft.com/library/d...y/en-us/dnadonet/html/advoracledataaccess.asp


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
G

Guest

Paul Clement said:
On Sun, 6 Feb 2005 21:21:02 -0800, "(e-mail address removed)"

¤ Hi,
¤
¤ I need the help deperately.
¤
¤ My requirement is that i have to retrieve BLOB object from the Oracle and
¤ need to save it on the Hard Disk through ADO.NET.
¤
¤ For this purpose i have tried it with OLEDB.NET Data provider and also
¤ through the oracle client.
¤
¤ As i know that BLOB object contains Microsoft Word Document i am saving it
¤ through Filestream object.
¤

See if the following helps:

http://msdn.microsoft.com/library/d...y/en-us/dnadonet/html/advoracledataaccess.asp


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)



Hi Paul,

Thanks for the reply.

In the MSDN article it mentions the way to retreive the BLOB object which
you can see in my code also.

Problem is that the Doc file which i am saving through FileStream is not
opening in the Word 2000 properly and it gives the message that word is
unable to read the specified format and it shows some junk characters in the
file although it's no. of pages as well as size matches the original document.

Please help.

Thanks & Regards,
Neeraj
 
P

Paul Clement

On Tue, 8 Feb 2005 21:41:02 -0800, "(e-mail address removed)"

¤
¤
¤ "Paul Clement" wrote:
¤
¤ > On Sun, 6 Feb 2005 21:21:02 -0800, "(e-mail address removed)"
¤ >
¤ > ¤ Hi,
¤ > ¤
¤ > ¤ I need the help deperately.
¤ > ¤
¤ > ¤ My requirement is that i have to retrieve BLOB object from the Oracle and
¤ > ¤ need to save it on the Hard Disk through ADO.NET.
¤ > ¤
¤ > ¤ For this purpose i have tried it with OLEDB.NET Data provider and also
¤ > ¤ through the oracle client.
¤ > ¤
¤ > ¤ As i know that BLOB object contains Microsoft Word Document i am saving it
¤ > ¤ through Filestream object.
¤ > ¤
¤ >
¤ > See if the following helps:
¤ >
¤ > http://msdn.microsoft.com/library/d...y/en-us/dnadonet/html/advoracledataaccess.asp
¤ >
¤ >
¤ > Paul ~~~ (e-mail address removed)
¤ > Microsoft MVP (Visual Basic)
¤ >
¤
¤
¤
¤ Hi Paul,
¤
¤ Thanks for the reply.
¤
¤ In the MSDN article it mentions the way to retreive the BLOB object which
¤ you can see in my code also.
¤
¤ Problem is that the Doc file which i am saving through FileStream is not
¤ opening in the Word 2000 properly and it gives the message that word is
¤ unable to read the specified format and it shows some junk characters in the
¤ file although it's no. of pages as well as size matches the original document.
¤

How was the document saved to the database? Have you been able to use another other method to
retrieve it (such as ADO)?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
G

Guest

I am using following code for retreival of Blob Data.
Using this code I am getting the DOCfile saves on the Disc.
It's size also matches with the original document.

But when i open that file it gives me the following message:

"Microsoft Word can't import the specified format.An Error occured andthis
feature no more functioning properly.Please Contact your System
Administrator."

It seems that code is working properly and retreiving the BLOB object also
from the Oracle but either there is problem with Filestream Object in writing
the byte array to file or may be somethign else..

Please help...


OracleLob blob = oraImgReader.GetOracleLob(1);

// Create a byte array of the size of the Blob obtained
Byte[] b = new Byte[blob.Length];

// Read blob data into byte array
int i = blob.Read(b,0,System.Convert.ToInt32(blob.Length));

System.IO.FileStream fs =new System.IO.FileStream("C:\\test25.doc",
System.IO.FileMode.Create, System.IO.FileAccess.Write);
fs.Write(b, 0, b.Length);
fs.Close();
 
C

Cor Ligthert

Paul,

With this question I had to think at your message "the ofset for a word
oledb blob is 85"
(Just an idea, I saw it is Oracle, however you never know)

For the rest I leave it up to you.

Cor
 
G

Guest

Please help it's very urgent!!!!

I am using following code for retreival of Blob Data.
Using this code I am getting the DOCfile saves on the Disc.
It's size also matches with the original document.

But when i open that file it gives me the following message:

"Microsoft Word can't import the specified format.An Error occured andthis
feature no more functioning properly.Please Contact your System
Administrator."

It seems that code is working properly and retreiving the BLOB object also
from the Oracle but either there is problem with Filestream Object in writing
the byte array to file or may be somethign else..

Please help...


OracleLob blob = oraImgReader.GetOracleLob(1);

// Create a byte array of the size of the Blob obtained
Byte[] b = new Byte[blob.Length];

// Read blob data into byte array
int i = blob.Read(b,0,System.Convert.ToInt32(blob.Length));

System.IO.FileStream fs =new System.IO.FileStream("C:\\test25.doc",
System.IO.FileMode.Create, System.IO.FileAccess.Write);
fs.Write(b, 0, b.Length);
fs.Close();

Paul Clement said:
On Tue, 8 Feb 2005 21:41:02 -0800, "(e-mail address removed)"

¤
¤
¤ "Paul Clement" wrote:
¤
¤ > On Sun, 6 Feb 2005 21:21:02 -0800, "(e-mail address removed)"
¤ >
¤ > ¤ Hi,
¤ > ¤
¤ > ¤ I need the help deperately.
¤ > ¤
¤ > ¤ My requirement is that i have to retrieve BLOB object from the Oracle and
¤ > ¤ need to save it on the Hard Disk through ADO.NET.
¤ > ¤
¤ > ¤ For this purpose i have tried it with OLEDB.NET Data provider and also
¤ > ¤ through the oracle client.
¤ > ¤
¤ > ¤ As i know that BLOB object contains Microsoft Word Document i am saving it
¤ > ¤ through Filestream object.
¤ > ¤
¤ >
¤ > See if the following helps:
¤ >
¤ > http://msdn.microsoft.com/library/d...y/en-us/dnadonet/html/advoracledataaccess.asp
¤ >
¤ >
¤ > Paul ~~~ (e-mail address removed)
¤ > Microsoft MVP (Visual Basic)
¤ >
¤
¤
¤
¤ Hi Paul,
¤
¤ Thanks for the reply.
¤
¤ In the MSDN article it mentions the way to retreive the BLOB object which
¤ you can see in my code also.
¤
¤ Problem is that the Doc file which i am saving through FileStream is not
¤ opening in the Word 2000 properly and it gives the message that word is
¤ unable to read the specified format and it shows some junk characters in the
¤ file although it's no. of pages as well as size matches the original document.
¤

How was the document saved to the database? Have you been able to use another other method to
retrieve it (such as ADO)?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top