c# - sql语法错误,以及与sql语法相关的字符串问题

标签 c# asp.net mysql sql html

嘿,大家好,当我将 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("&nbsp&nbsp " + "{0}", reader.GetString(0))));
                div.Attributes.Add("onclick", "return confirm_delete();");

                div.Style["clear"] = "both";
                test1.Controls.Add(div);

            }
        }
    }

我的数据库看起来像这样:

enter image description here

在我的代码中,我尝试将 div.ID 设置为 WallPosting 表中的当前 idWallPosting,所以我也不确定我是否正确。

编辑:

错误:

Unable to cast object of type 'System.Int32' to type '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的说明:

Gets the value of the column at the specified ordinal in its native format.

关于c# - sql语法错误,以及与sql语法相关的字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5489680/

相关文章:

c# - 当 RadioButtonList selectedindexchanged 事件在 asp.net 中没有触发时该怎么办?

asp.net - 同一解决方案中的项目之间如何共享类?

c# - ASPNETZERO - 数据表问题

php - 重复检查两个非常大的表

c# - 转换 DateTime 时不支持系统 - DotNetHighChart

c# - 如何获取 Windows 7 中缓存和释放的物理内存量?

c# - 在 C# 中使用正则表达式在字符串中 append 匹配项

asp.net - 解决大型 RewriteMap 配置部分

php - 基于从 ajax 调用返回的获取变量构建查询

mysql - 错误 1045 (28000) : Access denied for user 'root' @'localhost' (using password: YES) on Ubuntu (Zorin OS)