在下一页显示来自数据库的相同图像
本文关键字:图像 数据库 一页 显示 | 更新日期: 2023-09-27 18:11:56
我在一个网站上工作,我面临一个问题。我是初学者,所以请不要生我的气。我的问题是:我上传了一个图像文件到数据库中,并同时显示在框中。现在我想要的是,当我跳转到下一页时,同样的图片会出现在那里的框中,所以我可以调整它的大小。
到目前为止,我所做的是下面这段代码,但没有成功,它给出了错误必须声明标量变量"@id"
我在谷歌上搜索了一个解决方案,但没有找到适合我的解决方案。如有任何帮助,我将不胜感激。
ASPX部分为:
<asp:Image ID="image"
runat="server"
ImageUrl='<%# Eval("image") %>'
width="450" />
CS部分:
SqlConnection con = new SqlConnection("Data Source=Tim-PC''SQLEXPRESS;
Initial Catalog=AP_Data;
Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT image
FROM photo
WHERE id=@id",
con);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
DataBind();
}
你必须创建一个@Id参数…
SqlCommand cmd = new SqlCommand("SELECT image FROM photo WHERE id=@id",con);
SqlParameter param = new SqlParameter();
param.ParameterName = "@id";
param.Value = "Value_Of_parameter"; //Example 1
cmd.Parameters.Add(param);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
尝试如下:
SqlCommand cmd = new SqlCommand("SELECT image FROM photo WHERE id=@id", con);
cmd.Parameters.AddWithValue("@id", paramVal);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
希望这对你有帮助!