Saving and retriving images in database through WS

M

Marco Pais

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
 
C

cfps.Christian

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

Marco Pais

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.
 

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