ajax幻灯片放映从数据库中获取图像

时间:2010-03-24 09:32:43

标签: c#

我有侧面显示ajax,我希望它从数据库获取图像 我使用sql server 2000,我有二进制图像

这是我从数据库中选择图像的代码

public class SlidShow : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        using (SqlConnection con = Connection.GetConnection())
        {
            string Sql = "Select image from SlideShowImage Where  Active=1 And Hig_Id=@Hig_Id";
            System.Data.SqlClient.SqlCommand com = new SqlCommand(Sql, con);
            com.CommandType= System.Data.CommandType.Text;
            com.Parameters.Add(Parameter.NewInt("@Hig_Id", context.Request.QueryString["Hig_ID"].ToString()));

            System.Data.SqlClient.SqlDataReader dr = com.ExecuteReader();
            if (dr.Read() && dr != null)
            {

                Byte[] bytes1 = (Byte[])dr["image"];

                context.Response.BinaryWrite(bytes1);

                dr.Close();
            }
        }
    }

  [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]

    public static AjaxControlToolkit.Slide[] GetSlides()
    {


        return new AjaxControlToolkit.Slide[] { 
            new AjaxControlToolkit.Slide("images/sharp_highlight_ref_img.jpg", "", ""),
             new AjaxControlToolkit.Slide("images/products_fridg_img.jpg", "", ""),
            new AjaxControlToolkit.Slide("images/sharp_home_highlight_img.jpg", "", "")


        };

    }
}

1 个答案:

答案 0 :(得分:1)

我会将图片加载为HttpHandler,必须在web.config中注册。你不需要Ajax来加载图像。您的javascript代码必须更改src代码的img属性才能显示新图片。

这是一个http处理程序的示例,它使用名为id的查询参数从MS SQL数据库加载博客。

public class IISHandler1 : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        int theID;
        if (!int.TryParse(context.Request.QueryString["id"], out theID))
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg"; // or gif/png depending on what type of image you have
        Stream strm = DisplayImage(theID);
        byte[] buffer = new byte[2048];
        int byteSeq = strm.Read(buffer, 0, 2048);
        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 2048);
        }
    }

    public Stream DisplayImage(int theID)
    {
        try
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
            string sql = "SELECT image FROM Table1 WHERE id = @ID";
            using (SqlCommand cmd = new SqlCommand(sql, connection) { CommandType = CommandType.Text })
            {
                cmd.Parameters.AddWithValue("@ID", theID);
                connection.Open();
                object theImg = cmd.ExecuteScalar();
                return new MemoryStream((byte[]) theImg);
            }
        }
        catch
        {
            return null;
        }
    }
}