0

I want to create table in my database after button click. In Button_Click function I have a code

SqlConnection conn = new SqlConnection(@"MyConnectionString");

conn.Open();

SqlCommand cmd = new SqlCommand("CREATE TABLE '" + tableName+ "' (IdPy INT IDENTITY(1,1), Question NVARCHAR (MAX) NOT NULL, IsChecked BIT NOT NULL,  CONSTRAINTPK_'" + tableName+ "' PRIMARY KEY(Id) )", conn);

cmd.ExecuteNonQuery();
conn.Close();

tableName is my String variable (its value 2018-04-18 asd - yes, I want the table with such a name). And I have an error after button click:

System.Data.SqlClient.SqlException: 'Incorrect syntax near '2018-04-18 asd'.'

I think that the problem is in my SqlCommand. I would be gratefull if you could help me solve that problem.

10
  • 1
    Did you check the text of the SqlCommand through a debugger? Commented Apr 18, 2018 at 9:07
  • 1
    Please, check tableName value (put a breakpoint) Commented Apr 18, 2018 at 9:07
  • you may find you need to put [] round the table name not single quotes Commented Apr 18, 2018 at 9:07
  • My tableName value is "2018-04-18 asd" Commented Apr 18, 2018 at 9:08
  • Check CONSTRAINTPK_'" + tableName+ "' ... That would lead to CONSTRAINTPK_'myTable' Commented Apr 18, 2018 at 9:08

3 Answers 3

2

It looks like the tableName variable is 2018-04-18 asd. If that really is the correct table name, you need to escape it (and the constraint) in square brackets:

SqlCommand cmd = new SqlCommand("CREATE TABLE [" + tableName + "] (IdPy INT IDENTITY(1,1), Question NVARCHAR (MAX) NOT NULL, IsChecked BIT NOT NULL, CONSTRAINT [CONSTRAINTPK_" + tableName+ "] PRIMARY KEY(Id) )", conn);
Sign up to request clarification or add additional context in comments.

3 Comments

I got this error: System.Data.SqlClient.SqlException: 'The definition for column 'CONSTRAINTPK_2018-04-18asd' must include a data type.'
@MrNobody I wonder if you did miss an space BIT NOT NULL, CONSTRAINT_MISSING_SPACE_PK_
@bradbury9 You are right. It work's. Thank you for your feedback. :)
2

You should escape ([...] in case of MS SQL) table and constraint names:

  //DONE: wrap IDisposable into using
  using(SqlConnection conn = new SqlConnection(@"MyConnectionString")) {
    conn.Open();

    //DONE: Make sql readable. Can you see that you've skipped CONSTRAINT keyword?
    string sql = 
      $@"CREATE TABLE [{tableName}] (
           -- Fields
           IdPy      INT IDENTITY(1,1),
           Question  NVARCHAR (MAX)     NOT NULL, 
           IsChecked BIT                NOT NULL,

           -- Constraints
           --DONE: Constraint key word (optional in some RDBMS) added 
           CONSTRAINT [CONSTRAINTPK_{tableName}] PRIMARY KEY(Id) 
         )";

    //DONE: wrap IDisposable into using
    using (qlCommand cmd = new SqlCommand(sql, conn)) {
      cmd.ExecuteNonQuery();
    }
  } 

Comments

0

It might be easier to identify issues with your SQLCommand by using a string variable and parameterised string formatting. An example:

string query = "CREATE TABLE @tablename (IdPy INT IDENTITY(1,1), 
Question NVARCHAR (MAX) NOT NULL, IsChecked BIT NOT NULL,  
CONSTRAINTPK_@tablename PRIMARY KEY(Id) )";

string param = new {@tablename = txttable.txt(example)};

SqlCommand cmd = new SqlCommand(query, param, conn);

This might help step through to make sure that the variable you have to inspect more concise.

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.