日期:2014-05-17 浏览次数:20501 次
//此处略去using
namespace prjSRI01
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class getimage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // Is id empty?
//            if(!String.IsNullOrEmpty(context.Request.QueryString["Id"]))
            if (!String.IsNullOrEmpty(context.Request.QueryString["xingming"]))
            {
                 // No, retrieve the image from the database.
                SqlConnection sqlCon = new SqlConnection();
                SqlCommand    sqlCmd = new SqlCommand();
                SqlDataReader sqlRdr;
                string imageType;
                
                if(context.Request.QueryString["isThumb"].Equals("0"))
                    imageType = "Thumbnail"; // get thumbnail from the database.
                else
                    imageType = "Image";     // get original image from the database.
                sqlCon.ConnectionString = 
                     ConfigurationManager.ConnectionStrings["dbSampleConnectionString"].ConnectionString.ToString();
                //sqlCon.ConnectionString = @"Data Source=ARISJB\SQLEXPRESS;" +
                //                           "Initial Catalog=dbSample;" +
                //                           "Integrated Security=True";
                if(sqlCon.State.Equals(ConnectionState.Closed))
                    sqlCon.Open();
                
//                sqlCmd.CommandText = String.Concat("SELECT ", imageType, " FROM tblSRI WHERE ID=@Id");
              sqlCmd.CommandText = String.Concat("SELECT ", imageType, " FROM tblSRI WHERE Username=@xm");
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.Connection  = sqlCon;
//                sqlCmd.Parameters.AddWithValue("@Id", context.Request.QueryString["Id"]);
              sqlCmd.Parameters.AddWithValue("@xm", context.Request.QueryString["xingming"]);
                // Run the query.
                sqlRdr = sqlCmd.ExecuteReader(); 
                // Is record found?
                if(sqlRdr.HasRows)
                    // Yes, read the first record.
                    if(sqlRdr.Read())
                        // Return the image in binary format.
                        if (!sqlRdr[imageType].Equals(System.DBNull.Value ))
                        context.Response.BinaryWrite((byte[])sqlRdr[imageType]);
                sqlRdr.Close();    
            
                if(sqlCon.State.Equals(ConnectionState.Open))
                    sqlCon.Close();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}