Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET CSHTML-“关键字“user”附近的语法不正确。”_Asp.net_.net_Sql_Razor_Webmatrix - Fatal编程技术网

ASP.NET CSHTML-“关键字“user”附近的语法不正确。”

ASP.NET CSHTML-“关键字“user”附近的语法不正确。”,asp.net,.net,sql,razor,webmatrix,Asp.net,.net,Sql,Razor,Webmatrix,我正在尝试学习如何使用CSHTML和.NET对网站进行简单的数据库连接和查询输出,但从连接字符串到实际查询,我遇到了很多问题。请帮助我找到问题所在,并使此基本查询正常工作 数据库=Microsft SQL Express schema=id int,用户nchar10,密码char32 Web.Config的内容 下面是我失败的查询尝试。我试图使用w3学校的例子,但没有用 ListProducts.cshtml的内容 每次我跑的时候都会出现这个错误 Server Error in '/' App

我正在尝试学习如何使用CSHTML和.NET对网站进行简单的数据库连接和查询输出,但从连接字符串到实际查询,我遇到了很多问题。请帮助我找到问题所在,并使此基本查询正常工作

数据库=Microsft SQL Express schema=id int,用户nchar10,密码char32

Web.Config的内容

下面是我失败的查询尝试。我试图使用w3学校的例子,但没有用

ListProducts.cshtml的内容

每次我跑的时候都会出现这个错误

Server Error in '/' Application.
Incorrect syntax near the keyword 'user'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'.

Source Error:


Line 14: <th>password</th> 
Line 15: </tr>
Line 16: @foreach(var row in db.Query(selectQueryString))
Line 17: {
Line 18: <tr> 

猜测问题与数据库初始目录有关

Data Source=LAPTOP\\SQLEXPRESS;Initial Catalog=databasename

您的数据库真的命名为databasename吗?

结果表明它一定与我的数据库有关。我有一个名为user的表,其中有一个列也叫user。我将表user重命名为users,更新了代码,现在一切正常

尝试将用户括在方括号中:

var selectQueryString = "SELECT * from [user]";

用户在SQL中。

错误“关键字“user.”附近的语法不正确”使我认为它与该特定部分无关。虽然我开始认为这与我正在检索的数据类型有关,但我选择了一个不同的表,这很好,我会改进,这也起到了作用。我现在讨厌用户和它所代表的一切。@Jon我在试图找到这个事实的链接之前发布了答案,我当时不是100%确定。编辑我的答案以获得更多的确定性;
Data Source=LAPTOP\\SQLEXPRESS;Initial Catalog=databasename
var selectQueryString = "SELECT * from [user]";