Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Saving and retriving images in database through WS

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marco Pais - 20 Mar 2008 12:17 GMT
Hi there.

I'm developing a small application that uses Web Services to access database
and store data.

At some point, I store some images, by inserting the absolute image path
into a varchar field (SQL Server 2005). Images themselves are copied to that
path on the server. To read the images, I just use this path.

However, some workstations have to authenticate the server that path. To
avoid other problems, like deleting those images, I want to store images
directly in database, on a Image field.

So, after this long intro, I could have this code on server side (WS), to
save images:

/****/
[WebMethod]
public void SaveImage( string name, byte[] imageData )
{
  //use the web.config to store the connection string
  SqlConnection connection = new
SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
  SqlCommand command = new SqlCommand( "INSERT INTO Image(name, data)
VALUES (@name, @data)", connection );
  SqlParameter pName = new SqlParameter( "@name", SqlDbType.VarChar,50 );
  pName.Value = name;
  command.Parameters.Add( pName );
  SqlParameter pData = new SqlParameter( "@data", SqlDbType.Image );
  pData.Value = imageData;
  command.Parameters.Add( pData );
  try
  {
     connection.Open();
     int numRowsAffected = command.ExecuteNonQuery();
  }
  finally
  {
     connection.Close();
  }
}
/****/

and on client side:

/****/
remoteService.ServiceSoapClient srv = new remoteService.ServiceSoapClient();
Image image = Image.FromFile(@"c:\myimage.bmp");
using(MemoryStream stream = new MemoryStream())
{
  image.Save( stream );
  byte[] buffer = steam.ToByteArray();
  srv.SaveImage( "myimage", buffer );
}
/****/

My questions: is this good practice? Is there a better way to do it?

Thanks in advance. Sorry the long post.

Marco
cfps.Christian - 20 Mar 2008 14:17 GMT
Looks like you've got the right idea if you want to do it that way.
You mentioned that you have workstations using this telling me its
potentially an internal app, if thats the case what we did when I had
a similar issue was save the image/file to a shared directory on the
server and put the path to the image in the database.
Marco Pais - 20 Mar 2008 16:54 GMT
Right bow, I'm using a shared folder on the server. The problem is that
workstations aren't connected to a domain controler (windows home editons
versions only), and the first time workstations try to access server, thay
must authenticate.

Well, "regular" computer users, can't deal well with this, as you can
imagine.

> Looks like you've got the right idea if you want to do it that way.
> You mentioned that you have workstations using this telling me its
> potentially an internal app, if thats the case what we did when I had
> a similar issue was save the image/file to a shared directory on the
> server and put the path to the image in the database.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.