关键字"ORDER"附近的语法不正确. [英] Incorrect syntax near the keyword 'ORDER'.

查看:92
本文介绍了关键字"ORDER"附近的语法不正确.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关键字"ORDER"附近的语法不正确.


亲爱的,
我收到与上述相同的错误,但不知道如何解决.
这是我的代码:

Incorrect syntax near the keyword 'ORDER'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'ORDER'.

Dear all, 
I get the error same as the above but no idea how to solve it.
Here is my code:

 private void DisplayINASecKeyword(string Shortcode)
        {
            INADAC objINAKeyword = new INADAC();
            DataTable dt = new DataTable();
            dt = objINAKeyword.getINASecKeywordList(Shortcode);
            ddlINASecKeyword.DataSource = dt;
            ddlINASecKeyword.DataTextField = "seckeyword";
            ddlINASecKeyword.DataTextField = "seckeyword";
            ddlINASecKeyword.DataBind();
        }

 protected void ddlShortcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlCountry.SelectedItem.Value == "1" && ddlBilling.SelectedItem.Value == "0") //Malaysia and Premium SMS Selected
                {
                    ddlMYKeyword.Items.Clear();
                    DisplayMYKeyword(ddlShortcode.SelectedItem.Text);

                    ddlMYKeyword.Items.Insert(0, new ListItem("All", "?"));
                    this.ddlMYKeyword.SelectedIndex = 0; //Select "All" by default

                    ddlMYSecKeyword.Items.Clear();
                    DisplayMYSecKeyword(ddlShortcode.SelectedItem.Text);

                    ddlMYSecKeyword.Items.Insert(0, new ListItem("All", "?"));
                    this.ddlMYSecKeyword.SelectedIndex = 0; //Select "All" by default
                }
                else if(ddlCountry.SelectedItem.Value == "4" && ddlBilling.SelectedItem.Value == "0") //Indonesia and Premium SMS Selected
                {
                    ddlINAKeyword.Items.Clear();
                    DisplayINDOKeyword(ddlShortcode.SelectedItem.Text);

                    ddlINAKeyword.Items.Insert(0, new ListItem("All", "?"));
                    this.ddlINAKeyword.SelectedIndex = 0;

                    ddlINASecKeyword.Items.Clear();
                    DisplayINASecKeyword(ddlShortcode.SelectedItem.Text);

                    ddlINASecKeyword.Items.Insert(0, new ListItem("All", "?"));
                    this.ddlINASecKeyword.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                Logger.LogToFile(ConfigurationManager.AppSettings.Get("errLogPath") + "MISLog.txt", ex);
                throw;
            }
        }

public DataTable getINASecKeywordList(String Shortcode)
        {
            DataTable dt = new DataTable();
            Database dbCon = DatabaseFactory.CreateDatabase("InaPremium");
            string cmd = "";
            string keyword = null;

            try
            {
              
               if (Shortcode == "All" || Shortcode == "")
                {
                  
                   cmd = "SELECT DISTINCT(seckeyword) FROM INA_Keyword ORDER BY seckeyword";
                }
                else
                {
                   
                    cmd = "SELECT DISTINCT(seckeyword) FROM INA_Keyword WHERE shortcode = "+ Shortcode + " AND keyword = " + keyword + " ORDER BY seckeyword";
                }
                using (DbCommand dbcmd = dbCon.GetSqlStringCommand(cmd))
                {
                    dbcmd.CommandType = CommandType.Text;
                    dbcmd.CommandTimeout = 100;
                    dt = dbCon.ExecuteDataSet(dbcmd).Tables[0];
                }
                return dt;
            }
            catch (Exception ex)
            {
                Logger.LogToFile(ConfigurationManager.AppSettings.Get("errLogPath") + "INADAC.txt", ex);
                throw;
            }
        }



请帮忙..谢谢您的帮助.



I have no idea for the "Incorrect syntax near the keyword 'ORDER'

Please help..and thanks for your helping hand.


I'm newbie in enterprise as developer.

推荐答案

该错误表明您的SQL SELECT语句格式错误.我建议例如使用有问题的SELECT语句

The error indicates that your SQL SELECT statement is malformed. I would suggest taking the offending SELECT statement e.g.

cmd = "SELECT DISTINCT(seckeyword) FROM INA_Keyword WHERE shortcode = "+ Shortcode + " AND keyword = " + keyword + " ORDER BY seckeyword";

通过Console.WriteLine将其写入IDE输出窗口,在项目中创建一个.sql(添加一个新的文本文件,然后从.txt更改为.sql)文件,然后在该文件中运行查询,该问题应该会暴露出来.

Write it to the IDE output window via Console.WriteLine, create a .sql (add a new text file and change from .txt to .sql) file in your project and run the query there, the problem should expose itself.

经验法则不是使用字符串连接来创建where条件,而是使用 参数.

A rule of thumb is not to create your where condition using string concatenation but instead use parameters.


这篇关于关键字"ORDER"附近的语法不正确.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆