SQL错误关键字'user'附近的语法不正确[重复]
Posted
技术标签:
【中文标题】SQL错误关键字\'user\'附近的语法不正确[重复]【英文标题】:SQL error Incorrect syntax near the keyword 'user' [duplicate]SQL错误关键字'user'附近的语法不正确[重复] 【发布时间】:2016-06-14 08:40:34 【问题描述】:我正在尝试将 SQL Server 2008
连接到 c# windows 应用程序。
它给了我一个错误。
try
con = new SqlConnection("server=sameer-PC;database=Demo;Integrated Security=true;UID=admin;password=admin");
//String query = "select * from user";
con.Open();
cmd = new SqlCommand("select * from user", con);
Console.WriteLine("connection open0");
SqlDataReader myReader = null;
myReader = cmd.ExecuteReader();
while (myReader.Read())
Console.Write((myReader["name"]));
con.Close();
错误:
类型的第一次机会异常
'System.Data.SqlClient.SqlException' occurred in System.Data.dll
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'user'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject 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.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReadeThe thread 'vshost.RunParkingWindow' (0x820) has exited with code 0 (0x0).
The thread '<No Name>' (0xc80) has exited with code 0 (0x0).
【问题讨论】:
"select * from [user]" 可能会更好user
是保留关键字,您应该将其括在方括号中,如重复答案中的示例。
【参考方案1】:
user
是 SQL 中的 reserved keyword。所以你需要用方括号或Delimited Identifiers像这样select * from [user]
括起来
【讨论】:
如果这个答案对您有帮助,请考虑accepting it。接受答案会奖励那些帮助过你的人,它会帮助有同样问题的其他人找到正确的答案。【参考方案2】:你可以试试这个:
try
con = new SqlConnection("server=sameer-PC;database=Demo;Integrated Security=true;UID=admin;password=admin");
con.Open();
cmd = new SqlCommand("select * from [user]", con);
Console.WriteLine("connection open0");
SqlDataReader myReader = null;
myReader = cmd.ExecuteReader();
while (myReader.Read())
Console.Write((myReader["name"]));
con.Close();
【讨论】:
以上是关于SQL错误关键字'user'附近的语法不正确[重复]的主要内容,如果未能解决你的问题,请参考以下文章
执行 INSERT 时出现 SQL 错误:“s”附近的语法不正确 [重复]
错误 [42000] [Microsoft][SQL Native Client][SQL Server]关键字“SET”附近的语法不正确