2

Here's my code:

string createString = "CREATE TABLE user (uid INT PRIMARY KEY IDENTITY(1,1), uname NVARCHAR(30), uemail NVARCHAR(30), upass NVARCHAR(20), ucontact INT(10))"; //YOUR SQL COMMAND TO CREATE A TABLE

SqlConnection connection = new SqlConnection("Data Source=DELL\\SQLEXPRESS;Integrated Security=True;Connect Timeout=1000000;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

SqlCommand create = new SqlCommand(createString, connection);

connection.Open();
create.ExecuteNonQuery();
connection.Close();

I get the following error:

Incorrect syntax near the keyword 'user'

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'.

2 Answers 2

5

user is a reserved keyword in T-SQL. You need to use it with square brackets like [user]

As best practice, change it to non-reserved word.

Also use using statement to dispose your connection and commands automatically instead of calling Close method manually.

using(var connection = new SqlConnection(connectionString))
using(var create = connection.CreateCommand())
{
     create.CommandText = "...";
     connection.Open();
     create.ExecuteNonQuery();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Table name user is predefined word, change table name or use [user] and also column ucontact has datatype int with size, int will not accept size. That should be ucontact INT Finaly your query will look like this

CREATE TABLE [user] (uid INT PRIMARY KEY IDENTITY(1,1), uname NVARCHAR(30), uemail NVARCHAR(30), upass NVARCHAR(20), ucontact INT)

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.