【问题标题】:Incorrect syntax near ')'. while creating table [closed]')' 附近的语法不正确。创建表时[关闭]
【发布时间】:2015-07-18 03:22:18
【问题描述】:

我是 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)";

有人可以告诉我这个问题的原因吗?如何解决?

【问题讨论】:

  • 外键必须指向某处...
  • 该查询很容易受到SQL Injection... ;(
  • @kape123:连接 SQL 字符串并不意味着它容易受到 SQL 注入攻击。这取决于Table2Name 变量的来源。而且由于这是CREATE TABLE语句中的表名,所以在这种情况下OP不能进行参数绑定。

标签: c# sql-server sql-server-2012


【解决方案1】:

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 需要一个表引用。

【讨论】:

  • 还有做“identity(1, 1)”意味着什么?
  • 表示从 1 开始自动生成主键,增量为 1
  • @myroman 为什么我不能做 AUTOINCREMENT 呢?当我这样做时有什么问题? (当我尝试时,该行出现错误:','附近的语法不正确)
  • @ajpr 。 . .一种是 MySQL 语法。另一种是 SQL Server 语法。数据库做事不同。
猜你喜欢
  • 1970-01-01
  • 2021-03-22
  • 2020-11-03
  • 2018-10-25
  • 2013-06-26
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
相关资源
最近更新 更多