')'附近语法不正确.创建表时

本文关键字:创建 不正确 语法 | 更新日期: 2023-09-27 18:03:14

我是sqlserver的新手,我遇到了一个错误,这是:

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 37
ClientConnectionId:d9ec7a79-87d2-40f9-83f9-dc7b08c05153

这个错误原因的代码是下面的连接字符串:

 string sqlStatement2 = "CREATE TABLE " + Table2Name + "" +
      "(line_id AUTOINCREMENT PRIMARY KEY ," + 
        "line_full_name CHAR(50) NOT NULL,"+
        " network_id INTEGER FOREIGN KEY)";
谁能告诉我这个问题的原因吗?

')'附近语法不正确.创建表时

autoincrement不是SQL Server关键字。我想你的意思是:

 string sqlStatement2 = "CREATE TABLE " + Table2Name + "" + 
                         "(line_id int not null identity(1, 1) PRIMARY KEY, " +
                          "line_full_name CHAR(50) NOT NULL," +
                          " network_id INTEGER FOREIGN KEY REFERENCES network(network_id))";

另外,FOREIGN KEY需要一个表引用