查询表达式'pProductId ='中的语法错误(缺少运算符)

时间:2012-12-28 05:16:29

标签: asp.net image ms-access syntax-error

您好我正在尝试检索包含ie的pProductImage。来自products表的〜/ Images / pic1.jpg(images url),并使用pProductId作为标识符,根据Id显示不同的图像。

这是我的cataglogue.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OleDb;


public partial class Catalogue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ImageId = Request.QueryString["Img"];

        string sqlText = "SELECT pProductImage FROM Products WHERE pProductId = " +ImageId;

        OleDbConnection mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString);
        OleDbCommand cmd = new OleDbCommand(sqlText, mDB);

        mDB.Open();
        OleDbDataReader rdr = cmd.ExecuteReader();
        if (rdr.Read())
        {
            Response.BinaryWrite((byte[])rdr["pProductImage"]);
        }
        mDB.Close();

    }
}

和我的cataglogue.aspx(图像控件中显示的图像)

<div>

    <img src="Catalogue.aspx?pProductId=2" alt="" />
    </div>

1 个答案:

答案 0 :(得分:0)

如果要将图像URL存储在数据库表中,则需要将其视为字符串数据 - 例如

代码隐藏中的

protected string GetProductImgUrl(int productId)
{
   var sqlText = "SELECT pProductImage FROM Products WHERE pProductId = " + productId.ToString();
   using(var mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString))
   {
       mDB.Open();
       var cmd = new OleDbCommand(sqlText, mDB);
       using (var rdr = cmd.ExecuteReader())
       {
          if (rdr.Read())
          {
            return ResolveUrl((rdr["pProductImage"]).ToString());
          }
       }
   }
   return ResolveUrl("~/images/NotAvailable.png");     // Not available image
}

在加价中

<div>
    <img src='<%= GetProductImgUrl(2) %>' alt="" />
</div>