-2

I am database beginner and I was doing AUTOINCREMENT on creating table, but the following error occurred:

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ','.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolea
n breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
ect stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand
 cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,
TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName,
Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSou
rce`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean
asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at KellenTechnology.Program.Main(String[] args) in c:\Users\Mohit\Documents\V
isual Studio 2013\Projects\KellenTechnology\KellenTechnology\Program.cs:line 39
ClientConnectionId:1629d6a5-8e66-445b-99b1-b009103d0343
♂Data base Connection Closed. 

My code to do this is :

 string sqlStatement = "CREATE TABLE " + Table1Name + "" 
                    + "(network_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "network_full_name VARCHAR(50) NOT NULL)";

Could some one please let me know what's wrong with it? I also found somewhere not null identity(1, 1) instead of autoincrement but how the two are different?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ajpr
  • 113
  • 1
  • 12
  • 1
    SQL Server does not have anything named "AUTOINCREMENT". The corresponding functionality is called an identity column, so use `IDENTITY (1, 1)` instead. – Lasse V. Karlsen Jul 18 '15 at 16:23
  • 1
    The difference between autoincrement and identity is that the first one is not valid and does not work... and the "somewhere" is from answer to your own identical question where it was pointed out that autoincrement is not valid in SQL Server – James Z Jul 18 '15 at 16:24
  • @JamesZ thanks but i don't understand why there is disappointing -1 for this question. Anyways thanks all. – ajpr Jul 18 '15 at 16:33

2 Answers2

3

MS SQL-Server's equivalent of auto incrementing is called identity:

string sqlStatement = "CREATE TABLE " + Table1Name + 
                    + "(network_id INTEGER NOT NULL IDENTITY(1,1) PRIMARY KEY,"
                    + "network_full_name VARCHAR(50) NOT NULL)";
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

try this for SQL Server as from Error It is Sql Server's Error

string sqlStatement = "CREATE TABLE " + Table1Name + "" 
                    + " (network_id INTEGER identity(1,1) PRIMARY KEY,"
                    + "network_full_name VARCHAR(50) NOT NULL)";

BEWARE OF SQL INJECTION

James Z
  • 12,209
  • 10
  • 24
  • 44
Dgan
  • 10,077
  • 1
  • 29
  • 51