Saving images in table in SQL CE

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, i am trying to display an image which should be retrieved from a table in SQL Ce. i don't know how to save/insert an image into a table. Sql Ce does support image types but i couldn't get any info on how to save it. Thanks a lot.
 
Hi Saba,

Here's some sameple code you can use. It reads the image col data and store
it in a local bitmap file, make a copy of the file (because of file
sharing) and set image control src to the copy. Might not be the most
elegant solution, but it works.

SqlCeDataReader reader = null;
SqlBinary binary = null;
FileStream f = File.Create ("pet.bmp");

//read current image from DB
if (this.command == null) this.command =
this.connection.CreateCommand();
this.command.CommandText = "SELECT pic from pets where
petname = foo";
reader = command.ExecuteReader();
while (reader.Read())
{
binary = reader.GetSqlBinary(0);
if (binary.IsNull == false)
{
f.Write (binary.Value, 0, binary.Length);
}
}
reader.Close();
f.Flush();
f.Close();

if (null != this.picPet.Image)
this.picPet.Image.Dispose();
File.Copy ("pet.bmp", "petcopy.bmp", true);

//display it in picture control
if (binary.Length > 0)
{
Bitmap photo = new Bitmap("petcopy.bmp");
this.picPet.Image = photo;
}
else
this.picPet.Image = null;

Thanks,
Amy

--------------------
| From: "James McCutcheon" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| References: <[email protected]>
| Subject: Re: Saving images in table in SQL CE
| Lines: 15
| Organization: j3 Technology
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Date: Thu, 8 Apr 2004 19:03:12 +1000
| NNTP-Posting-Host: 203.103.159.73
| X-Trace: nnrp1.ozemail.com.au 1081414992 203.103.159.73 (Thu, 08 Apr 2004
19:03:12 EST)
| NNTP-Posting-Date: Thu, 08 Apr 2004 19:03:12 EST
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08
..phx.gbl!newsfeed00.sul.t-online.de!t-online.de!news.glorb.com!meganewsserve
rs.com!feeder2.on.meganewsservers.com!newshosting.com!nx02.iad01.newshosting
..com!uunet!dca.uu.net!snewsf0.syd.ops.aspac.uu.net!nnrp1.ozemail.com.au!53ab
2750!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:50519
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| You have to store it as a byte array ...
|
| See Alex's article for more details ...
| http://www.opennetcf.org/forums/topic.asp?TOPIC_ID=194
|
| James McCutcheon
|
| | > Hi, i am trying to display an image which should be retrieved from a
table
| in SQL Ce. i don't know how to save/insert an image into a table. Sql Ce
| does support image types but i couldn't get any info on how to save it.
| Thanks a lot.
|
|
|
 
Thanks a lot, though a few of the classes were not supported in VB. anyways, thanks a lot , i figured it out!!
 
Back
Top