简体   繁体   English

SQL语法错误? 并在我的c#img url字符串中

[英]error in sql syntax? and in my c# img url string

Hi I have an sql syntax error: 嗨,我有一个SQL语法错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '[User] u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHE' at line 1 检查与您的MySQL服务器版本相对应的手册,以找到在'[User] u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHE'附近使用正确的语法

My code: 我的代码:

using (OdbcCommand cmd = new OdbcCommand("SELECT 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 UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
            {
                //("SELECT 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 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.Style["float"] = "left";

                        div.ID = "test";
                        Image img = new Image();
                        img.ImageUrl = String.Format("{1}", reader.GetString(0));
                        // this line needs to be represented in sql syntax
                        img.AlternateText = "Test image";

                        div.Controls.Add(img);
                        div.Controls.Add(ParseControl(String.Format("{0}", reader.GetString(0))));
                        div.Style["clear"] = "both";
                        test1.Controls.Add(div);

                    }
                }
            }
        }
    }

My database structure: 我的数据库结构:

在此处输入图片说明

EDIT: 编辑:

If i take the brackets out of the User in mysql syntax I then get an error: 如果我使用mysql语法从用户中删除括号,则会收到错误消息:

Column 'UserID' in where clause is ambiguous where子句中的“ UserID”列不明确

RE EDIT: 重新编辑:

adding wp. 添加wp。 to UserID in WHERE clause gives a new error: WHERE子句中的UserID出现新错误:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list. 索引(从零开始)必须大于或等于零且小于参数列表的大小。

Im sure this is due to this line: 我确定这是由于以下原因:

img.ImageUrl = String.Format("{1}", reader.GetString(0));

Replace this: 替换为:

WHERE UserID=

With this: 有了这个:

WHERE wp.UserID=

UserID exists in multiple tables so you have to fully qualify it in the WHERE clause. UserID存在于多个表中,因此您必须在WHERE子句中完全限定它。

          V
... WHERE wp.UserID= ...
          ^

You use both Picture and User in your query, both of which have a UserId column. 您在查询中同时使用PictureUser ,它们都有一个UserId列。 You'll need to tell your query which one to use. 您需要告诉您的查询使用哪个查询。

Eg. 例如。

WHERE wp.UserId = 

EDIT 1 编辑1

Also, 也,

In your String.Format calls, replace "{1}" with "{0}" . 在您的String.Format调用中,将"{1}"替换为"{0}" "{1}" tells Format to look for the 2nd value you're passing, not the first (since it's zero based). "{1}"告诉Format来查找您要传递的第二个值,而不是第一个(因为它是从零开始的)。

EDIT 2 编辑2

As for the original issue (for completeness' sake), remove the square brackets from around [User] in the SQL in your first line. 对于原始问题(出于完整性考虑),请在第一行中从SQL的[User]周围删除方括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM