My issue is that I have created a table for a local db in Visual Studio 10.0 using C# and I want to have an auto-incrementing field which I have setup. The table is created and the identity field seems to be setup correctly and everything seems fine, but I always get a syntax error on the insert when I use an identity field — so I imagine I have a stupid little typo, but perhaps this is just not supported using VS?
This is the create table string and execution:
-connection string omitted-
string createTableString = @"CREATE TABLE [Test](
[Test_ID] [int] IDENTITY (1,1) PRIMARY KEY NOT NULL,
[OtherField] [varchar] (50))";
SqlCommand command = new SqlCommand(createTableString, con);
command.ExecuteNonQuery();
And this is what I’m using to insert into the table:
string insertString = @"INSERT INTO Test VALUES ('sometext')";
SqlCommand command1 = new SqlCommand(insertString, con);
command1.ExecuteNonQuery();
Note that I’ve tried this as well and get the same syntax error:
string insertString = @"INSERT INTO Test (OtherField) VALUES ('sometext')";
SqlCommand command1 = new SqlCommand(insertString, con);
command1.ExecuteNonQuery();
Also note that for this project I must do this via these scripting commands in Visual Studio. Is this possible? Thanks very much ahead of time.
The scripts look correct, I can only assume it has to do with the owner of the table. SQL makes the current user the owner of the table. Try [youruser].[Test] and see if that helps.