【问题标题】:incorrect syntax near '=' error'=' 错误附近的语法不正确
【发布时间】:2013-01-16 09:42:25
【问题描述】:

我只想从我的数据库中检索图像路径,我在运行时提供了表名,但是 id 也出现了问题,它给了我在 '=' 附近语法不正确的错误

这是我的查询

string query = "select strImage from " + tableName + "where intID ="+Id;

【问题讨论】:

    标签: sql select


    【解决方案1】:

    您需要在WHERE 子句之前添加额外的空格,

    string query = "SELECT strImage FROM " + tableName + " WHERE intID ="+Id;
                                                       -- ^ HERE
    

    假设变量tableName的值为Hello,当它被连接起来时,查询将是这样的,

    SELECT strImage FROM HelloWHERE intID =0
                          --  ^ lacking space here
    

    【讨论】:

      【解决方案2】:

      我希望您的查询是正确的。语法有点问题。试试这个

      string query = "select strImage from " + tableName + " where intID ="+Id;
      

      【讨论】:

        【解决方案3】:
        string query = String.Format("SELECT strImage FROM {0} WHERE intID = {2}", tableName, Id);
        

        字符串的连接导致创建多个对象

        【讨论】:

        • 如果 OP 使用的是.net,这很好
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多