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