【问题标题】:ajax slide show get images from databaseajax幻灯片从数据库中获取图像
【发布时间】:2025-12-01 17:10:01
【问题描述】:

我有杂耍 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", "", "")


        };

    }
}

【问题讨论】:

  • 请编辑此内容并添加一个问题(它们是以“?”结尾的问题),您还应该向我们展示一些代码,让我们知道您在做什么。最好指出您尝试过的内容以及收到的错误。

标签: c#


【解决方案1】:

我会将图像加载为HttpHandler,它必须在 web.config 中注册。您实际上并不需要 Ajax 来加载图像。您的 JavaScript 代码必须更改 img 标记的 src 属性才能显示新图像。

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

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;
        }
    }
}

【讨论】:

  • 感谢您的关注,但如果您使用 java 代码或所有示例进行分配。
  • 我还不清楚您还需要什么帮助?幻灯片中的图像 url 必须由您在 web.config 中为 http 句柄注册的内容触发。例子。注册一个模式,如:/images/slide.ahx?id=。并在您的代码中引用这样的图像: new AjaxControlToolkit.Slide("images/slide.ahx?id=1".. etc.