/* Here is the SQL Table structure and procedure for reference.
create database test_Imagedb
use test_Imagedb
create table image_tbl(imgname varchar(100), my_image image)
alter procedure Insert_Image (
@image_name varchar(100),
@picture image)
as
begin
insert into image_tbl values(@image_name,@picture)
end
create procedure Get_Image_Name
as
begin
select * from image_tbl
end
create procedure Get_Image(@image_name varchar(100))
as
begin
select * from image_tbl where imgname=@image_name
end
*/
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Data.SqlClient;
using System.Data;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
DataSet ds;
SqlCommand cmd;
SqlConnection conn;
SqlDataAdapter sda;
string connstr = @"Data Source=kannan;Initial Catalog=test_Imagedb;Persist Security Info=True;User ID=sa;Password=cgvak123";
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
/*
* Web Method Name : Retrive_Image_Name
* Description : This method is used to retrive the image name, image from the data table,
* the value from the table is returned in dataset.
* StoredProcedure : Get_Image_Name
* Return Type : DataSet
*
* */
[WebMethod]
public DataSet Retrive_Image_Name()
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
ds = new DataSet();
cmd = new SqlCommand("Get_Image_Name", conn);
cmd.CommandType = CommandType.StoredProcedure;
sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
/*
* Web Method Name : Retrive_Image
* Description : This method is used to retrive the image from the data table according to the Image Name.
* Actually the image is converted in to bytes and image is returned in the form of bytes.
* StoredProcedure : Get_Image
* Return Type : Byte Array
*
* */
[WebMethod]
public byte[] Retrive_Image(string simagename)
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
ds = new DataSet();
cmd = new SqlCommand("Get_Image", conn);
cmd.Parameters.Add("image_name", SqlDbType.VarChar, 100).Value = simagename;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader sdr;
sdr = cmd.ExecuteReader();
byte[] file = null;
while (sdr.Read())
{
file = (byte[])sdr["my_image"];
}
sdr.Close();
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(file, 0, file.Length);
Context.Response.Buffer = true;
Context.Response.BinaryWrite(file);
memoryStream.Dispose();
return file;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
/*
* Web Method Name : updateImagedata
* Description : This method is used to upload the image name and image in to the table.
* Image name and the image is passed as argument.
* StoredProcedure : Get_Image
* Return Type : Void
*
* */
[WebMethod]
public void updateImagedata(string sPicName, byte[] data)
{
string imagename = sPicName;
try
{
if (imagename != "")
{
conn = new SqlConnection(connstr);
conn.Open();
cmd = new SqlCommand("Insert_Image", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("image_name", SqlDbType.VarChar, 100).Value = imagename;
cmd.Parameters.Add("picture", SqlDbType.Image).Value = data;
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}