简体   繁体   中英

Incorrect syntax near 'FROM' Keyword. C#

I have tried debugging of code and commenting, but I can't seem to find the problem. I am getting the this error:

Incorrect syntax near 'FROM'.

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 'FROM'.

My code:

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;
}

Suggest you to format string properly like using string format as below and also check tablename is present or not

 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>();
  }

Add [] around table name because table name in sql may contains white space example. SELECT * FROM [My Table] , that also can cause issue (check this answer for more detail : SELECT query on a table with a space in the name using SQSH )

As @PranayRana mention in comments to his answer, this issue is mostly because of that tableName parameter, passed to GetTableColumns() , is null , or empty string. I'm 98% sure.

You need to check parameter value before you use it. But instead returning a default value (empty list, for example), I prefer throwing an exception (because it is, literally , exceptional situation), like this:

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;
}

One more thing. You also need to handle escaping of your SQL commands. For example, you can't just SELECT * FROM User; with MS SQL Server, you need to SELECT * FROM [User]; , or even SELECT * FROM [dbo].[User]; sometimes. This may vary from DBMS to DBMS.

As advise, I recomend you to learn something about ORMs , like EntityFramework or NHibernate , because you will be then free from this DBMS -relative stuff like syntax, or escaping sequences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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