0

I am trying to create a table pragmatically, my code generates the following syntax:

string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL"

But every time I try to run the code, I generate the following exception.

My code:

   SQLCommand cmd = new SQLCommand(x,conn);
   cmd.ExecuteNonQuery();

The exception I get:

There was an error parsing the query. [ Token line number = 1,Token line offset = 66,Token in error = NULL ]

I am using SqlCeConnection if that makes a difference, and SqlCeCommand for execution.

Am I creating the table incorrectly?

My backend connection and execution looks like this :

public bool ExecuteSqlCommand(string sqlStr, IList<SqlCeParameter> parameters, SqlCeConnection conn)
{
   SqlCeCommand cmd = null;
   cmd = conn.CreateCommand();

   try
   {
      if (parameters != null)
      {
         foreach (SqlCeParameter param in parameters)
         {
             cmd.Parameters.Add(param);
         }
      }

      cmd.CommandText = sqlStr;

      int affectedRows = cmd.ExecuteNonQuery();

      bool DDLCommand = sqlStr.StartsWith("Drop ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Delete ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Insert ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Update ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Create ", StringComparison.OrdinalIgnoreCase);
      DDLCommand = DDLCommand || sqlStr.StartsWith("Alter ", StringComparison.OrdinalIgnoreCase);

      if (DDLCommand || affectedRows > 0)
         return true;
      else
         return false;
   }
   catch (Exception e)
   {
       SqlCeException sqlEx = e as SqlCeException;
   }

   if (cmd != null)
       cmd.Dispose();

   return false;
}
4
  • 2
    Have you opened the connection? Commented Jul 23, 2013 at 13:41
  • If you run the command directly against the DB (e.g. in SQL Server Management Studio), does it work? You appear to be missing a closing parenthesis. Commented Jul 23, 2013 at 13:42
  • 1
    It seems that ")" is missing at the end of the SQL query (string x): string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"; // <- ")" Commented Jul 23, 2013 at 13:42
  • Shouldn't you be using a SqlCeCommand, then? A good practice is to just use connection.CreateCommand(). Commented Jul 23, 2013 at 13:42

2 Answers 2

7

You're not closing your (

string x = "CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"
Sign up to request clarification or add additional context in comments.

Comments

5

Your query is missing )

"CREATE TABLE [test] ([ID] int NOT NULL,[Name] nvarchar(100) NOT NULL)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.