当我在下面的代码中将idWallPosting
添加到我的select语句时,嘿家伙得到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));
答案 0 :(得分:1)
你需要一个逗号。这样:
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 (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))
逗号:--------------------------------------------- -------------------------------------------------- ----- ^
编辑:
您可以尝试GetValue
而不是GetString
。 GetValue的描述:
以原始格式获取指定序号的列的值。
答案 1 :(得分:0)
wallPosting是一个int,你正在使用.GetString。你可能应该使用.GetInt32。