【问题标题】:Incorrect syntax near 'FROM' Keyword. C#“FROM”关键字附近的语法不正确。 C#
【发布时间】:2018-07-03 05:57:29
【问题描述】:

我尝试过调试代码和注释,但似乎找不到问题所在。 I am getting the this error:

“FROM”附近的语法不正确。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:“FROM”附近的语法不正确。

我的代码:

public List<string> GetTableColumns(string tableName)
{
    List<string> colCollection = new List<string>();

    IDataReader reader = null;

    using (var cmd = MyDB.GetSqlStringCommand("SELECT * FROM " + tableName))
    {
        using (reader = MyDB.ExecuteReader(cmd))
        {
            foreach (DataRow r in reader.GetSchemaTable().Rows)
            {
                colCollection.Add(r["ColumnName"].ToString());
            }

            reader.Close();
        }
    }

    return colCollection;
}

【问题讨论】:

  • 你调试过这个看看tableName到底是什么吗?去做那件事并报告
  • 表的名称是什么以及作为tableName 传递的内容是什么?
  • OT 如果您只想要列,请添加 WHERE 1=0。你显然不想要任何行,那么为什么要得到它们呢?
  • 添加了我的答案你可以试试
  • 我认为查询中没有错误。你能发布GetSqlStringCommand的功能细节吗?

标签: c# asp.net sql-server


【解决方案1】:

建议您像使用下面的字符串格式一样正确格式化字符串,并检查表名是否存在

 public List<string> GetTableColumns(string tableName)
 {
   if(!String.IsNullOrWhiteSpace(tableName))
   {
    string query = string.Format( "SELECT * FROM  [{0}]",tableName);
    // or use string interpolation
    string query = $"SELECT * FROM [{tableName}]";
    //rest of the code execute reader  
    //return collection of string   
   }
   return new List<string>();
  }

在表名周围添加[],因为sql中的表名可能包含空格示例。 SELECT * FROM [My Table] ,这也可能导致问题(查看此答案以获取更多详细信息:SELECT query on a table with a space in the name using SQSH

【讨论】:

  • 这和"SELECT * FROM " + tableName是一样的,为什么你认为这会有帮助?
  • @vasily.sib - 但它更清晰,我添加了代码来检查 tableName 是否为空
  • 我非常怀疑more clear。并且检查空是没有意义的。 "some string " + string.Empty"some string " + null$"some string {null}" 将产生相同的字符串
  • @vasily.sib - 你是对的,但如果你发送“select * from(空字符串中没有值)”,那么 sql 会给你错误,这也是问题的原因,根据 OP 在问题
  • @vasily.sib - 根据这个Incorrect syntax near 'FROM'.,如果没有给出表值或表值包含错误输入,则可能导致错误
【解决方案2】:

正如@PranayRana 在 cmets 中提到他的回答,这个问题主要是因为传递给 GetTableColumns()tableName 参数是 null 或空字符串。我有 98% 的把握。

使用前需要检查参数值。但不是返回默认值(例如空列表),我更喜欢抛出异常(因为它是,字面意思,异常情况),像这样:

public List<string> GetTableColumns(string tableName)
{
    if (tableName == null)
        throw new ArgumentNullException(nameof(tableName));
    if (string.IsNullOrWhitespace(tableName))
        throw new ArgumentException("Table name can't be empty!", nameof(tableName));

    List<string> colCollection = new List<string>();

    using (var cmd = MyDB.GetSqlStringCommand($"SELECT * FROM {tableName}"))
    using (var reader = MyDB.ExecuteReader(cmd))
        foreach (DataRow r in reader.GetSchemaTable().Rows)
            colCollection.Add(r["ColumnName"].ToString());

    return colCollection;
}

还有一件事。您还需要处理 SQL 命令的转义。例如,你不能只SELECT * FROM User; 与 MS SQL Server,你需要SELECT * FROM [User];,有时甚至需要SELECT * FROM [dbo].[User];。这可能因 DBMS 而异。

作为建议,我建议您学习一些关于 ORM 的知识,例如 EntityFrameworkNHibernate,因为这样您就可以摆脱这种与 DBMS 相关的东西了比如语法,或者转义序列。

【讨论】:

    猜你喜欢
    • 2015-10-10
    • 1970-01-01
    • 2020-12-18
    • 2013-08-02
    • 2016-06-08
    • 2013-12-16
    • 2015-12-04
    • 2017-11-22
    相关资源
    最近更新 更多