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.
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.