E
Eric Paul
I searched for the answer to this question when I started this new
project and I couldn't find a specific example of uploading an image
into SQL via a web service from a Windows Application. I found a ton
of other examples which got me close, but weren't exactly what I was
looking for.
As is my policy, when I look for an answer and don't find one I try to
publish mine (beware bad line breaks).
In your web service add the following:
[WebMethod]
public int SaveImage(Byte[] imgdata)
{
//use the web.config to store the connection string
SqlConnection connection = new SqlConnection("YourConnectionString");
//build the SQL command
//Put your insert statement here--the "Image" column needs to
//be of type "image"
SqlCommand command = new SqlCommand( "INSERT INTO ContactsImages
(ImageData) VALUES (@img_data )", connection );
//build the parameters
command.Parameters.Add(new SqlParameter("@img_data", imgdata));
//open the datbase connection
try
{
//if the DB connection is not already open, open it
if(connection.State != ConnectionState.Open)
{
connection.Open();
}
}
catch
{
//can't open the conenction to the database--throw an error
Exception oException= new Exception("An error occured while opening
connection to the database.");
throw oException;
}
//perform the sql command and store the results
int numRowsAffected = command.ExecuteNonQuery();
//close the database connection
connection.Close();
//answer with the number of rows - 1 if successful
return numRowsAffected;
}
Then in your windows application add a "web refrence" to the web
service and use code like the following:
private void btnUpload_Click(object sender, System.EventArgs e)
{
String strBLOBFilePath = textBox1.Text; //textbox with name and path
to image
FileStream fsBLOBFile = new
FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close();
MYWEBSERVICE.MYSERVICENAME IDS = new MYWEBSERVICE.MYSERVICENAME();
int Output = IDS.SaveImage(bytBLOBData);
}
The key is to convert the image to a byte[] array before you send it.
Hope someone else finds this useful.
project and I couldn't find a specific example of uploading an image
into SQL via a web service from a Windows Application. I found a ton
of other examples which got me close, but weren't exactly what I was
looking for.
As is my policy, when I look for an answer and don't find one I try to
publish mine (beware bad line breaks).
In your web service add the following:
[WebMethod]
public int SaveImage(Byte[] imgdata)
{
//use the web.config to store the connection string
SqlConnection connection = new SqlConnection("YourConnectionString");
//build the SQL command
//Put your insert statement here--the "Image" column needs to
//be of type "image"
SqlCommand command = new SqlCommand( "INSERT INTO ContactsImages
(ImageData) VALUES (@img_data )", connection );
//build the parameters
command.Parameters.Add(new SqlParameter("@img_data", imgdata));
//open the datbase connection
try
{
//if the DB connection is not already open, open it
if(connection.State != ConnectionState.Open)
{
connection.Open();
}
}
catch
{
//can't open the conenction to the database--throw an error
Exception oException= new Exception("An error occured while opening
connection to the database.");
throw oException;
}
//perform the sql command and store the results
int numRowsAffected = command.ExecuteNonQuery();
//close the database connection
connection.Close();
//answer with the number of rows - 1 if successful
return numRowsAffected;
}
Then in your windows application add a "web refrence" to the web
service and use code like the following:
private void btnUpload_Click(object sender, System.EventArgs e)
{
String strBLOBFilePath = textBox1.Text; //textbox with name and path
to image
FileStream fsBLOBFile = new
FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close();
MYWEBSERVICE.MYSERVICENAME IDS = new MYWEBSERVICE.MYSERVICENAME();
int Output = IDS.SaveImage(bytBLOBData);
}
The key is to convert the image to a byte[] array before you send it.
Hope someone else finds this useful.