【发布时间】:2011-07-26 06:29:36
【问题描述】:
大家好,当我在下面的代码中的 select 语句中添加 idWallPosting 时,我遇到了一个 sqlsyntax 错误:
using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting, wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
{
using (OdbcDataReader reader = cmd.ExecuteReader())
{
test1.Controls.Clear();
while (reader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Attributes["class"] = "test";
div.ID = String.Format("{0}", reader.GetString(2));
// this line is responsible, problem here and my sqlsntax, im trying to set the SELECT idWallPosting for the div ID
Image img = new Image();
img.ImageUrl = String.Format("{0}", reader.GetString(1));
img.AlternateText = "Test image";
div.Controls.Add(img);
div.Controls.Add(ParseControl(String.Format("   " + "{0}", reader.GetString(0))));
div.Attributes.Add("onclick", "return confirm_delete();");
div.Style["clear"] = "both";
test1.Controls.Add(div);
}
}
}
我的数据库如下所示:
在我的代码中,我试图将 div.ID 设置为 WallPosting 表中的当前 idWallPosting,所以我也不确定我是否正确。
编辑:
错误:
无法将“System.Int32”类型的对象转换为“System.String”类型。
我认为与这一行有关:
div.ID = String.Format("{0}", reader.GetString(2));
【问题讨论】:
-
开始吧,idwallposting 后你是不是少了一个逗号?