BLOB fields and Microsoft Access

R

Randel Bjorkquist

I'm having a problem pulling out a Microsoft Excel Workbook from a Microsoft
Access database. I know I have the database set up correct, because from
withinside of Access I can insert and retreive the files. What is happening
is that when I try to to them out via my code, I get the error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables["tbl_File"];
DataRow TmpRow = TmpTable.Rows[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

*******************************************************
THE ERROR HAPPENS HERE
*******************************************************
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
*******************************************************
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------
 
E

Elton Wang

Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)
 
R

Randel Bjorkquist

Hey Elton,

I've made some change and now do not get any errors. But it's still not
working correctly. Whether the file is large or small, I always get 293 KB.
One file I'm trying to get is only 46 KB and the other is 204 KB. Do you
have any ideas of what is happening?

Thanks for the help,

Randel Bjorkquist

//-----------------------------------------------------------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables["tbl_File"];
DataRow TmpRow = TmpTable.Rows[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];

bw.Write(TemplateFile, 0, TemplateFile.Length -1);
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------



Elton Wang said:
Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)


-----Original Message-----
I'm having a problem pulling out a Microsoft Excel Workbook from a Microsoft
Access database. I know I have the database set up correct, because from
withinside of Access I can insert and retreive the files. What is happening
is that when I try to to them out via my code, I get the error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

*******************************************************
THE ERROR HAPPENS HERE
*******************************************************
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
*******************************************************
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------



.
 
E

Elton Wang

Hi Randel,

You can use small buffer size (you set it to 1000000),
e.g. 1024, for processing. If it still gets trouble,
please let me know.


HTH

Elton Wang
-----Original Message-----
Hey Elton,

I've made some change and now do not get any errors. But it's still not
working correctly. Whether the file is large or small, I always get 293 KB.
One file I'm trying to get is only 46 KB and the other is 204 KB. Do you
have any ideas of what is happening?

Thanks for the help,

Randel Bjorkquist

//---------------------------------------------------- -------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];

bw.Write(TemplateFile, 0, TemplateFile.Length - 1);
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------



Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)


-----Original Message-----
I'm having a problem pulling out a Microsoft Excel Workbook from a Microsoft
Access database. I know I have the database set up correct, because from
withinside of Access I can insert and retreive the files. What is happening
is that when I try to to them out via my code, I get the error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

*******************************************************
THE ERROR HAPPENS HERE
*******************************************************
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
*******************************************************
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD ------------------------------------------------- -



.


.
 
R

Randel Bjorkquist

Hey Elton,

Thanks for the information, but I'm still having problems. I would have
replied sooner, I ended up running to school for about 4 hours and am now
back for the rest of my day. My code has changed from my first post, so if
you could look this over and let me know what you think is going wrong, I'd
greatly appreciated.

Here is a quick run down of my application. I'm writing my app using C#
(Visual Studio .NET) and I'm trying to connect to a Microsoft Access
database. I found this article
http://support.microsoft.com/kb/317701/EN-US/ and started here.

I've now gotten my application to create two different size files. One is
205 KB and the other is still 293 KB.

Again, I thank you for your help and am looking forward to seeing what I'm
doing wrong.

Randel

//-----------------------------------------------------------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables["tbl_File"];
DataRow TmpRow = TmpTable.Rows[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;
byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader(CommandBehavior.SequentialAccess);
myReader.Read();

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];
myReader.GetBytes(0, 0, TemplateFile, 0, TemplateFile.Length);

myReader.Close();
olecon_BLOB.Close();

fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);

bw.Write(TemplateFile, 0, TemplateFile.Length);
bw.Close();
fs.Close();

Successful = true;
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------





Elton Wang said:
Hi Randel,

You can use small buffer size (you set it to 1000000),
e.g. 1024, for processing. If it still gets trouble,
please let me know.


HTH

Elton Wang
-----Original Message-----
Hey Elton,

I've made some change and now do not get any errors. But it's still not
working correctly. Whether the file is large or small, I always get 293 KB.
One file I'm trying to get is only 46 KB and the other is 204 KB. Do you
have any ideas of what is happening?

Thanks for the help,

Randel Bjorkquist

//---------------------------------------------------- -------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];

bw.Write(TemplateFile, 0, TemplateFile.Length - 1);
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------



Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)



-----Original Message-----
I'm having a problem pulling out a Microsoft Excel
Workbook from a Microsoft
Access database. I know I have the database set up
correct, because from
withinside of Access I can insert and retreive the
files. What is happening
is that when I try to to them out via my code, I get the
error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables
["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

*******************************************************
THE ERROR HAPPENS HERE
*******************************************************
retval = myReader.GetBytes(1, startIndex,
outbyte, 0, bufferSize);
*******************************************************
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex,
outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD ------------------------------------------------- -



.


.
 
R

Randel Bjorkquist

My mistake, it was this article:

http://www.dotnet247.com/247reference/a.aspx?u=http://support.microsoft.com/?kbid=317016

Randel

Randel Bjorkquist said:
Hey Elton,

Thanks for the information, but I'm still having problems. I would have
replied sooner, I ended up running to school for about 4 hours and am now
back for the rest of my day. My code has changed from my first post, so
if you could look this over and let me know what you think is going wrong,
I'd greatly appreciated.

Here is a quick run down of my application. I'm writing my app using C#
(Visual Studio .NET) and I'm trying to connect to a Microsoft Access
database. I found this article
http://support.microsoft.com/kb/317701/EN-US/ and started here.

I've now gotten my application to create two different size files. One is
205 KB and the other is still 293 KB.

Again, I thank you for your help and am looking forward to seeing what I'm
doing wrong.

Randel


//-----------------------------------------------------------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables["tbl_File"];
DataRow TmpRow = TmpTable.Rows[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;
byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader(CommandBehavior.SequentialAccess);
myReader.Read();

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];
myReader.GetBytes(0, 0, TemplateFile, 0, TemplateFile.Length);

myReader.Close();
olecon_BLOB.Close();

fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);

bw.Write(TemplateFile, 0, TemplateFile.Length);
bw.Close();
fs.Close();

Successful = true;
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------





Elton Wang said:
Hi Randel,

You can use small buffer size (you set it to 1000000),
e.g. 1024, for processing. If it still gets trouble,
please let me know.


HTH

Elton Wang
-----Original Message-----
Hey Elton,

I've made some change and now do not get any errors. But it's still not
working correctly. Whether the file is large or small, I always get 293 KB.
One file I'm trying to get is only 46 KB and the other is 204 KB. Do you
have any ideas of what is happening?

Thanks for the help,

Randel Bjorkquist

//---------------------------------------------------- -------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];

bw.Write(TemplateFile, 0, TemplateFile.Length - 1);
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------



Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)



-----Original Message-----
I'm having a problem pulling out a Microsoft Excel
Workbook from a Microsoft
Access database. I know I have the database set up
correct, because from
withinside of Access I can insert and retreive the
files. What is happening
is that when I try to to them out via my code, I get the
error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables
["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

*******************************************************
THE ERROR HAPPENS HERE
*******************************************************
retval = myReader.GetBytes(1, startIndex,
outbyte, 0, bufferSize);
*******************************************************
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex,
outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD ------------------------------------------------- -



.



.
 
R

Randel Bjorkquist

Hey Elton,

I think I've found the issue, but I don't know how to solve it. I ended up
putting a simple text file into my database and trying to pull that out just
like I was doing on the Excel workbooks. Just like the Excel workbooks, its
size grew. I tried to open it and found a tons of garbage in it. So I'm
guessing that Microsoft Access wraps the data so it knows how and what to
open them as.

Again, I believe not understanding this is my problem. So if you, or anyone
else, has any ideas, please let me know. I just find it hard to beleive
that I'm the first person who wants to do something like this.

Oh well, thanks again for your help and maybe you'll still have another idea
I can try.

Randel


Elton Wang said:
Hi Randel,

You can use small buffer size (you set it to 1000000),
e.g. 1024, for processing. If it still gets trouble,
please let me know.


HTH

Elton Wang
-----Original Message-----
Hey Elton,

I've made some change and now do not get any errors. But it's still not
working correctly. Whether the file is large or small, I always get 293 KB.
One file I'm trying to get is only 46 KB and the other is 204 KB. Do you
have any ideas of what is happening?

Thanks for the help,

Randel Bjorkquist

//---------------------------------------------------- -------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];

bw.Write(TemplateFile, 0, TemplateFile.Length - 1);
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------



Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)



-----Original Message-----
I'm having a problem pulling out a Microsoft Excel
Workbook from a Microsoft
Access database. I know I have the database set up
correct, because from
withinside of Access I can insert and retreive the
files. What is happening
is that when I try to to them out via my code, I get the
error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables
["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

*******************************************************
THE ERROR HAPPENS HERE
*******************************************************
retval = myReader.GetBytes(1, startIndex,
outbyte, 0, bufferSize);
*******************************************************
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex,
outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD ------------------------------------------------- -



.


.
 
E

Elton Wang

Try following code:

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;
byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);
//
if(myReader.Read())
{
long size = myReader.GetBytes(0,0,null,0,0);
int chunkSize = 1024*100;
TemplateFile = new byte[size];
long bytesRead = 0;
int pos =0;
if (size <= chunkSize)
{
myReader.GetBytes(0,0,TemplateFile,0,(int)size);
}
else
{
while (bytesRead < size)
{
bytesRead += myReader.GetBytes
(0,pos,TemplateFile,pos,chunkSize);
pos += chunkSize;
}
}
//
myReader.Close();
olecon_BLOB.Close();

fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);

bw.Write(TemplateFile, 0, TemplateFile.Length);
bw.Close();
fs.Close();

Successful = true;
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
\}//END OF "GetTemplateFile"

I don't test the code. There are maybe some errors. But
the logic is there.

HTH

Elton Wang
-----Original Message-----
Hey Elton,

Thanks for the information, but I'm still having problems. I would have
replied sooner, I ended up running to school for about 4 hours and am now
back for the rest of my day. My code has changed from my first post, so if
you could look this over and let me know what you think is going wrong, I'd
greatly appreciated.

Here is a quick run down of my application. I'm writing my app using C#
(Visual Studio .NET) and I'm trying to connect to a Microsoft Access
database. I found this article
http://support.microsoft.com/kb/317701/EN-US/ and started here.

I've now gotten my application to create two different size files. One is
205 KB and the other is still 293 KB.

Again, I thank you for your help and am looking forward to seeing what I'm
doing wrong.

Randel

//---------------------------------------------------- -------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;
byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);
myReader.Read();

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];
myReader.GetBytes(0, 0, TemplateFile, 0, TemplateFile.Length);

myReader.Close();
olecon_BLOB.Close();

fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);

bw.Write(TemplateFile, 0, TemplateFile.Length);
bw.Close();
fs.Close();

Successful = true;
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------





Hi Randel,

You can use small buffer size (you set it to 1000000),
e.g. 1024, for processing. If it still gets trouble,
please let me know.


HTH

Elton Wang
-----Original Message-----
Hey Elton,

I've made some change and now do not get any errors.
But
it's still not
working correctly. Whether the file is large or small,
I
always get 293 KB.
One file I'm trying to get is only 46 KB and the other
is
204 KB. Do you
have any ideas of what is happening?

Thanks for the help,

Randel Bjorkquist

//--------------------------------------------------
--
-------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];

bw.Write(TemplateFile, 0,
TemplateFile.Length -
1);
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD ------------------------------------------------- -



Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)



-----Original Message-----
I'm having a problem pulling out a Microsoft Excel
Workbook from a Microsoft
Access database. I know I have the database set up
correct, because from
withinside of Access I can insert and retreive the
files. What is happening
is that when I try to to them out via my code, I get the
error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables
["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

****************************************************** *
THE ERROR HAPPENS HERE
****************************************************** *
retval = myReader.GetBytes(1, startIndex,
outbyte, 0, bufferSize);
****************************************************** *
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex,
outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD -----------------------------------------------
--
-


.
 
R

Randel Bjorkquist

Hey Elton,

Thanks for the help. But I'm sorry tell you that it didn't work. It's
still doing the same thing. I think Access is wrapping the inserted file
and I'm just simply picking that crap up. I don't know and I'm just
guessing. I've also put a post up one level explaining what I'm talking
about.

Thanks again for all your help,

Randel

Elton Wang said:
Try following code:

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;
byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);
//
if(myReader.Read())
{
long size = myReader.GetBytes(0,0,null,0,0);
int chunkSize = 1024*100;
TemplateFile = new byte[size];
long bytesRead = 0;
int pos =0;
if (size <= chunkSize)
{
myReader.GetBytes(0,0,TemplateFile,0,(int)size);
}
else
{
while (bytesRead < size)
{
bytesRead += myReader.GetBytes
(0,pos,TemplateFile,pos,chunkSize);
pos += chunkSize;
}
}
//
myReader.Close();
olecon_BLOB.Close();

fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);

bw.Write(TemplateFile, 0, TemplateFile.Length);
bw.Close();
fs.Close();

Successful = true;
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
\}//END OF "GetTemplateFile"

I don't test the code. There are maybe some errors. But
the logic is there.

HTH

Elton Wang
-----Original Message-----
Hey Elton,

Thanks for the information, but I'm still having problems. I would have
replied sooner, I ended up running to school for about 4 hours and am now
back for the rest of my day. My code has changed from my first post, so if
you could look this over and let me know what you think is going wrong, I'd
greatly appreciated.

Here is a quick run down of my application. I'm writing my app using C#
(Visual Studio .NET) and I'm trying to connect to a Microsoft Access
database. I found this article
http://support.microsoft.com/kb/317701/EN-US/ and started here.

I've now gotten my application to create two different size files. One is
205 KB and the other is still 293 KB.

Again, I thank you for your help and am looking forward to seeing what I'm
doing wrong.

Randel

//---------------------------------------------------- -------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables ["tbl_File"];
DataRow TmpRow = TmpTable.Rows [lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;
byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader (CommandBehavior.SequentialAccess);
myReader.Read();

TemplateFile = new byte[(myReader.GetBytes(0, 0, null, 0,
int.MaxValue))];
myReader.GetBytes(0, 0, TemplateFile, 0, TemplateFile.Length);

myReader.Close();
olecon_BLOB.Close();

fs = new FileStream("_tmp.xlt", FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);

bw.Write(TemplateFile, 0, TemplateFile.Length);
bw.Close();
fs.Close();

Successful = true;
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD --------------------------------------------------





Hi Randel,

You can use small buffer size (you set it to 1000000),
e.g. 1024, for processing. If it still gets trouble,
please let me know.


HTH

Elton Wang

-----Original Message-----
Hey Elton,

I've made some change and now do not get any errors. But
it's still not
working correctly. Whether the file is large or small, I
always get 293 KB.
One file I'm trying to get is only 46 KB and the other is
204 KB. Do you
have any ideas of what is happening?

Thanks for the help,

Randel Bjorkquist

//-------------------------------------------------- --
-------------------------------
private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables
["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

byte[] TemplateFile;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

TemplateFile = new byte[(myReader.GetBytes(0,
0, null, 0,
int.MaxValue))];

bw.Write(TemplateFile, 0, TemplateFile.Length -
1);
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(OleDbException E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "catch(Exception E)"

return(Successful);
}//END OF "GetTemplateFile"
METHOD ------------------------------------------------- -



in message
Hi Randel,

Try following

Change
retval = myReader.GetBytes(1, startIndex, outbyte, 0,
bufferSize);
to
retval = myReader.GetBytes(1, startIndex, null, 0,
bufferSize);

HTH

Elton Wang
(e-mail address removed)



-----Original Message-----
I'm having a problem pulling out a Microsoft Excel
Workbook from a Microsoft
Access database. I know I have the database set up
correct, because from
withinside of Access I can insert and retreive the
files. What is happening
is that when I try to to them out via my code, I get the
error "Index was
outside the bounds of the array".

Can anyone help me?

Thanks in advance,

Randel Bjorkquist

private bool GetTemplateFile(){
bool Successful = false;

DataTable TmpTable = dset_TemplateInfo.Tables
["tbl_File"];
DataRow TmpRow = TmpTable.Rows
[lstbox_TemplateNames.SelectedIndex];

FileStream fs;
BinaryWriter bw;

int bufferSize = 1000000;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;

try{
olecon_BLOB.Open();

olecom_BLOB.Parameters["FileID"].Value =
int.Parse(TmpRow["FileID"].ToString());
olecom_BLOB.Prepare();

OleDbDataReader myReader =
olecom_BLOB.ExecuteReader
(CommandBehavior.SequentialAccess);

while(myReader.Read()){
fs = new FileStream("_tmp.xlt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite);
bw = new BinaryWriter(fs);

startIndex = 0;

****************************************************** *
THE ERROR HAPPENS HERE
****************************************************** *
retval = myReader.GetBytes(1, startIndex,
outbyte, 0, bufferSize);
****************************************************** *
while(retval == bufferSize){
bw.Write(outbyte);
bw.Flush();

startIndex =+ bufferSize;
retval = myReader.GetBytes(1, startIndex,
outbyte, 0,
bufferSize);
}//END OF "while(retval == bufferSize)"

bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();

Successful = true;
}//END OF "while(myReader.Read())"

myReader.Close();
olecon_BLOB.Close();
}//END OF "" TRY-BLOCK
catch(Exception E){
MessageBox.Show(E.Message);
olecon_BLOB.Close();
}//END OF "finally"

return(Successful);
}//END OF "GetTemplateFile"
METHOD ----------------------------------------------- --
-



.



.


.
 
C

Cor Ligthert

Randal,

Try in your code when you are accessing a database which has pictures
wrapped by OLE to set the startindex to 78 and the length than to -78.

I hope this helps,

Cor
 
R

Randel Bjorkquist

Hey Cor,

Thanks for the idea to try. But unfortunately, I couldn't get that to work
either. I used the file "GoneFishing.bmp" from windows, which has the size
of 17 KB before I put it into the database. However, when I pull it back
out of the database, its size becomes 21 KB. And that gives me a bad
result.

Do you have any more ideas?

Randel
 
P

Paul Clement

¤ Hey Elton,
¤
¤ I think I've found the issue, but I don't know how to solve it. I ended up
¤ putting a simple text file into my database and trying to pull that out just
¤ like I was doing on the Excel workbooks. Just like the Excel workbooks, its
¤ size grew. I tried to open it and found a tons of garbage in it. So I'm
¤ guessing that Microsoft Access wraps the data so it knows how and what to
¤ open them as.
¤
¤ Again, I believe not understanding this is my problem. So if you, or anyone
¤ else, has any ideas, please let me know. I just find it hard to beleive
¤ that I'm the first person who wants to do something like this.
¤
¤ Oh well, thanks again for your help and maybe you'll still have another idea
¤ I can try.
¤

How is this file being inserted into the database?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
R

Randel Bjorkquist

Hey Paul,

I'm dragging and dropping it straight into the table. I first open the
Microsoft Access, then open the table in datasheet view and then drag and
drop the Microsoft Excel Workbook.

Long story short, I'm holding information about the workbook in this
database and was trying to hold the workbook itself, instead of having to
hold a full pathname. It's not a big application and really doesn't require
me to put it on the SQL Server.

Any help you can give would be great.

Thanks,

Randel
 
P

Paul Clement

¤ Hey Paul,
¤
¤ I'm dragging and dropping it straight into the table. I first open the
¤ Microsoft Access, then open the table in datasheet view and then drag and
¤ drop the Microsoft Excel Workbook.
¤
¤ Long story short, I'm holding information about the workbook in this
¤ database and was trying to hold the workbook itself, instead of having to
¤ hold a full pathname. It's not a big application and really doesn't require
¤ me to put it on the SQL Server.
¤
¤ Any help you can give would be great.
¤

The method you describe uses OLE embedding. When this occurs OLE header information is being added
to the data before it is stored. The format of the header information added is dependent upon the
native application associated with the file.

In order to extract the file in its native format the OLE header information needs to be removed.
Unfortunately the header information will vary depending upon the type of file you are working with
so it can be rather difficult to remove without displaying in an OLE object control (which isn't
supported in .NET).

For Office files the offset where the file starts is typically 78, although you can also search for
the "ÐÏ" character combination (without the double quotes). For bitmaps (not JPG) you can look for
"BM" to find the offset. I believe the offset is around 31.

The alternative to storing the file as an OLE Object is to store it and retrieve it as straight
binary w/o the OLE header information. I have code to do this if you are interested.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
R

Randel Bjorkquist

Hey Paul,

Thanks for all the information and yes, I'd be more then happy to look at
the code you are talking about. My email address is:
(e-mail address removed)

Just as an FYI, my intent is to only have Microsoft Excel Workbooks
Templates kept in this database. I also tried an offset of 78 KB. But
either I did something wrong, or it's not the right length.

Lastly, you made mention that in bitmap files, I could look for "BM" in the
file and that in the Microsoft Office products I could look for something
else, ÐÏ. What is this? What character values are these? I'm trying to
understand exactally what I should be searching for.

Thanks again and I look forward to receiving your code.

Randel
 
P

Paul Clement

¤ Hey Paul,
¤
¤ Thanks for all the information and yes, I'd be more then happy to look at
¤ the code you are talking about. My email address is:
¤ (e-mail address removed)
¤
¤ Just as an FYI, my intent is to only have Microsoft Excel Workbooks
¤ Templates kept in this database. I also tried an offset of 78 KB. But
¤ either I did something wrong, or it's not the right length.
¤
¤ Lastly, you made mention that in bitmap files, I could look for "BM" in the
¤ file and that in the Microsoft Office products I could look for something
¤ else, ÐÏ. What is this? What character values are these? I'm trying to
¤ understand exactally what I should be searching for.
¤
¤ Thanks again and I look forward to receiving your code.

The character string combinations I posted indicate where the actual file information begins (after
the OLE header info). Basically you search for the first occurrence of that combination and from
there you begin extracting the data. As I mentioned it's different based upon type of file that is
stored so it's a bit of a crapshoot unless you know the file type.

Below is the code I use. It stores the data in an Access OLE Object field but w/o the OLE headers
(no OLE embedding):

Sub WriteBlobToAccess()

Dim SourceFilePath As String
SourceFilePath = "e:\My Documents\Greenstone.bmp"
Dim AccessConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\db1.mdb")
Dim AccessCommand As New OleDbCommand("UPDATE Table1 SET OLEField=? WHERE [record id] = 1",
AccessConnection)
Dim FileStreamObject As New System.IO.FileStream(SourceFilePath, IO.FileMode.Open,
IO.FileAccess.Read)
Dim PictureByteArray(CType(FileStreamObject.Length() - 1, Integer)) As Byte
FileStreamObject.Read(PictureByteArray, 0, PictureByteArray.Length)
FileStreamObject.Close()
Dim QueryParameter As New OleDbParameter("@Picture", OleDbType.LongVarBinary,
PictureByteArray.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current,
PictureByteArray)
AccessCommand.Parameters.Add(QueryParameter)
AccessConnection.Open()
AccessCommand.ExecuteNonQuery()
AccessConnection.Close()

End Sub

Sub ReadBlobFromAccess()

Dim DestFilePath As String
Dim RetVal As Long
Dim FieldLen As Int32

DestFilePath = "e:\My Documents\GreenstoneFromAccess.bmp"
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim AccessConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\db1.mdb")
Dim AccessCommand As New OleDbCommand("SELECT [record ID], OLEField FROM Table1 WHERE
[record id] = 1", AccessConnection)
AccessConnection.Open()
Dim AccessDataReader As OleDbDataReader =
AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
AccessDataReader.Read()
FieldLen = AccessDataReader.Item(1).Length
Dim PictureByteArray(FieldLen - 1) As Byte
Dim StartIndex As Integer = 0
RetVal = AccessDataReader.GetBytes(1, StartIndex, PictureByteArray, 0,
PictureByteArray.Length)
AccessDataReader.Close()
AccessConnection.Close()
Dim FileStreamObject As New System.IO.FileStream(DestFilePath, IO.FileMode.Create,
IO.FileAccess.Write)
FileStreamObject.Write(PictureByteArray, 0, PictureByteArray.Length)
FileStreamObject.Close()

End Sub


Paul
~~~~
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