Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
mycmd = mycn.CreateCommand();
 mycmd.CommandText = "CREATE TABLE IF NOT EXISTS `ems_db1.dbo`.`querysummary` LIKE `ems_db1.dbo`.`" + TreeView1.SelectedNode.Text + "_querysummary`";

I Use above code in Mysql it work proper but now I shift database to SQLExpress after firing this query in code c# code it gives an following error.

Incorrect syntax near the keyword 'IF'.

Incorrect syntax near '`'.

Is my something wrong in my query?
Updated 16-Nov-15 19:26pm
Member 11922776 17-Nov-15 2:08am    
is my question wrong?
OriginalGriff 17-Nov-15 2:18am    
Have patience. It's early in the morning in Europe, and hassling people to answer you is not a good way to get volunteers to help you... 40 minutes is all you waited...
Member 11922776 17-Nov-15 2:34am    
It's afternoon in other countries. It is not the case that, I am asking to particular one.
George Jonsson 17-Nov-15 2:56am    
And how did you spend your 40 minutes?
Did you read the documentation for the syntax for creating a table in SQL Express?
Because if you did you would most likely have found the answer OriginalGriff has provided for you.

Connect to the DB, and try this:
IF object_id('querysummary', 'U') IS NOT NULL
    CREATE TABLE 'querysummary' ...
Share this answer
George Jonsson 17-Nov-15 2:58am    
Not so impatient when it comes to accept your solution. :-)
Maciej Los 17-Nov-15 3:37am    
In addition to solution 2 by OriginalGriff[^]... Do not use such of statements! Why?
1) your statement is Sql Injection[^] vulnerable,
2) you can't control the count of newly created tables
3) it requires admin privileges (be careful in giving full access to the inexperienced users!),
4) etc.

For further information, please see:
How To: Protect From SQL Injection in ASP.NET[^]
Do Stored Procedures Protect Against SQL Injection?[^]
SQL Injection and how to avoid it[^]
Share this answer
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='tbl_name' AND xtype='U')
    select * into ems_db1.dbo.kquerysummary from ems_db1.dbo.querysummary where 1 = 0
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900