Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've this code to create a table if it doesnt exists:

CSS
MessageBox.Show("create table");
                    conn.Open();
                    SqlCommand command = new SqlCommand("IF OBJECT_ID('UXMenu', 'U') IS NULL CREATE TABLE UXMenu(ID int PRIMARY KEY AUTOINCREMENT, TransDocument char(5), TransSerial char(5), TransDocNumber float, PartyName char(60), PartyLocalityID char(5), TotalAmount char (25), ShipToPostalCode char(35), Estado int);", conn); command.ExecuteNonQuery();
                    MessageBox.Show("tabela criada");
conn.close();


But this code isnt working, I get the first messagebox but then it just ignores the rest of the code and doesnt create anything.

What I have tried:

CSS
MessageBox.Show("create table");
                    conn.Open();
                    SqlCommand command = new SqlCommand("IF OBJECT_ID('UXMenu', 'U') IS NULL CREATE TABLE UXMenu(ID int PRIMARY KEY AUTOINCREMENT, TransDocument char(5), TransSerial char(5), TransDocNumber float, PartyName char(60), PartyLocalityID char(5), TotalAmount char (25), ShipToPostalCode char(35), Estado int);", conn);
                    command.ExecuteNonQuery();
                    MessageBox.Show("tabela criada");
conn.close();
Posted
Updated 25-Aug-17 5:56am
v3
Comments
Richard Deeming 25-Aug-17 11:04am    
Three options:

1) The code is executing;
2) The code is throwing an exception;
3) The code you're running isn't the code you're looking at;

If it's #2, then you need to show us the full details of the exception.

Debug your code and step through it line by line to see where it's going wrong.
Richard Deeming 25-Aug-17 11:07am    
NB: If you're using Microsoft SQL Server, you're most likely getting a SQL syntax exception relating to AUTOINCREMENT. That's a Microsoft Access keyword; in SQL, you use the IDENTITY property instead.
Member 13356973 25-Aug-17 11:09am    
1: The code isnt executing;
2: I dont get any exception;
3: The code I'm looking is the code that is running.
Jochen Arndt 25-Aug-17 11:14am    
If that is the code you are running:
Why do you execute
command.ExecuteNonQuery();

twice?
Member 13356973 25-Aug-17 11:14am    
It was the IDENTITY problem now its solved thanks

1 solution

SqlCommand command = new SqlCommand("IF OBJECT_ID('UXMenu', 'U') IS NULL CREATE TABLE UXMenu(ID int PRIMARY KEY IDENTITY(1,1), TransDocument char(5), TransSerial char(5), TransDocNumber float, PartyName char(60), PartyLocalityID char(5), TotalAmount char (25), ShipToPostalCode char(35), Estado int);", conn);
 
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