嗨,当我在下面的代码中将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));
最佳答案
您需要一个逗号。这个:
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的说明:以其本机格式获取指定序数列的值。
关于c# - sql语法错误,以及与sql语法相关的字符串问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5489680/