Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read image from accessdb

Status
Not open for further replies.

goransaler

IS-IT--Management
Mar 6, 2003
56
SE
Hello

I'm trying to read a image from a access database
and i get a file but it's corrupt and a little bigger then the original.

I hope someone can help me.
The code i'm working on looks like this





OleDbConnection con = new OleDbConnection(@"Jet OLEDB:<<this was very long so i cut it>>";


OleDbDataAdapter oleDbDA = new OleDbDataAdapter("Select * From Artist WHERE ID=1", con);

OleDbCommandBuilder MyCB = new OleDbCommandBuilder(oleDbDataAdapter2);

DataSet ds = new DataSet("Artist");
byte[] MyData= new byte[0];
oleDbDA.Fill(ds, "Artist");
DataRow myRow;
myRow=ds.Tables["Artist"].Rows[0];
MyData = (byte[])myRow["Photo"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);

FileStream fs = new FileStream(@"tmp.BMP", FileMode.OpenOrCreate, FileAccess.Write);

fs.Write(MyData, 0,ArraySize);
fs.Close();
 
hello again
i now understand that it's a header
that tells how to open the file
is there a way to get the blob without that.

hope for help
 
I found something that maked it work
but if i like to do it the other way is there
anyone that can help me whith that?

I have a Mp3CdoleDbConnection



OleDbCommand cmd = new OleDbCommand("SELECT Photo FROM Album WHERE id = 1", Mp3CdoleDbConnection);

MemoryStream ms = new MemoryStream();

// 78 is the size of the OLE header for Northwind images.
// There's no header in PUBS as PUBS
// just contains the raw image bits.
int offset = 78;

Mp3CdoleDbConnection.Open();
byte [] img = (byte[]) cmd.ExecuteScalar();
ms.Write(img, offset, img.Length-offset);

Mp3CdoleDbConnection.Close();

Bitmap bmp = null;
bmp = new Bitmap(ms);
AlbumphotopictureBox.Image= Image.FromStream(ms);

Thanks
 
The code below will extract all photos for the Access DB and create one .bmp file for each record.
For SQL DB use offset =0;
Code:
// ...
DataSet ds = new DataSet("Artist");
oleDbDA.Fill(ds, "Artist");
DataTable dt = ds.Tables["Artist"];
FileStream fs=null;
BinaryWriter bw = null;
int offset = 78; // OLE Object 
for (int i=0;i<dt.Rows.Count;i++)
{
	System.Array  photo = (System.Array)dt.Rows[i]["Photo"];
	byte[] outbyte = new byte[photo.Length]; 
	fs = new FileStream("C:\\photo" + dt.Rows[i]["name"].ToString() + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
	bw = new BinaryWriter(fs);
	bw.Write(outbyte,offset, photo.Length - offset);
	bw.Close();
	fs.Close();
	
}
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top