1

I am trying to create a table in my SQL Server CE database. I am receiving an error. I thought it was the ' in the name, but even running it with just the numbers I receive the same error. I did a print on the command and ran it over at: http://www.w3schools.com/sql/trysql.asp?filename=trysql_create_table and it worked fine. Can someone steer me into the right direction of what I am missing?

Here is the error:

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

Here is the code:

    public static void CreateDataTable(string dataBase, string tableName)
    {
        try
        {
            tableName = "John's License_191019";
            var createUser = string.Format(@"CREATE TABLE [{0}] (
                   [Identifier]  INT NOT NULL ,
                   [Id]  INT NULL ,
                   [Name]  NVARCHAR(255) NULL ,
                   [Zone]  INT NULL ,
                   [Map]  INT NULL ,
                   [State]  NVARCHAR(255) NULL ,
                   [Type]  NVARCHAR(255) NULL ,
                   [Faction]  NVARCHAR(255) NULL ,
                   [X]  FLOAT NULL ,
                   [Y]  FLOAT NULL ,
                   [Z]  FLOAT NULL ,
                   [Create_Date]  DATETIME NULL ,
                   [Update_Date]  DATETIME NULL ,
                   PRIMARY KEY ([Identifier]))", tableName);

            using (var conn = new SqlCeConnection(SQLHelper.Connection(dataBase)))
            {
                conn.Open();

                using (var cmd = new SqlCeCommand(createUser, conn))
                {
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ExecuteNonQuery: " + ex);
                    }
                    finally
                    {
                        cmd.Dispose();
                    }
                }

                conn.Close();
                conn.Dispose();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("CreateDataTable: " + ex);
            throw;
        }
    }

How the string prints:

CREATE TABLE [John's License_191019] ([Identifier] INT NOT NULL ,[Id] INT NULL ,[Name] NVARCHAR(255) NULL ,[Zone] INT NULL ,[Map] INT NULL ,[State] NVARCHAR(255) NULL ,[Type] NVARCHAR(255) NULL ,[Faction] NVARCHAR(255) NULL ,[X] FLOAT NULL ,[Y] FLOAT NULL ,[Z] FLOAT NULL ,[Create_Date] DATETIME NULL ,[Update_Date] DATETIME NULL ,PRIMARY KEY ([Identifier]))

1 Answer 1

1

The correct delimiters for Sql Compact Edition are not the backticks but the Open/Close square brackets

var createUser = @"CREATE TABLE [John's License_191019] (
                       [Identifier]  INT NOT NULL,
                       [Id]  INT NULL ,
                       [Name]  NVARCHAR(255) NULL ,
                       [Zone]  INT NULL ,
                       [Map]  INT NULL ,
                       [State]  NVARCHAR(255) NULL ,
                       [Type]  NVARCHAR(255) NULL ,
                       [Faction]  NVARCHAR(255) NULL ,
                       [X]  FLOAT NULL ,
                       [Y]  FLOAT NULL ,
                       [Z]  FLOAT NULL ,
                       [Create_Date]  DATETIME NULL ,
                       [Update_Date]  DATETIME NULL ,
                       PRIMARY KEY ([Identifier]))";

By the way, your code above don't need the string.Format (a typo?)

And two final notes.
An empty Try/Catch is a very bad practice. I suggest to remove it or handle the exception.
The using statement removes the need to call Close and Dispose on the objects involved.

Sign up to request clarification or add additional context in comments.

5 Comments

string format was me not removing it before pasting the code. The tablename is actually passed into the method. I was just using the table name "John's License_191019" as a test case. There is also code for the try catch that I removed. Essentially just logging code that was not relevant to the problem =).
I received this error: " There was an error parsing the query. [Token in line number = 1, Token line offset 13, Token in error 191019 ] " I used your code except this time I made it use the string.Format and made "John's Licenses_191019" a string.
Could you update your question with this last version of your createUser variable?
Sorry, but I can't reproduce. What happen if you use a table name without spaces and quotes embedded in its name?
The error was being tossed here: MessageBox.Show("ExecuteNonQuery: " + ex); It was my code.. your code was correct. Thank you for your time.

Your Answer

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