C# 使用asp.net从数据库检索数据(“用户”附近的语法不正确)

C# 使用asp.net从数据库检索数据(“用户”附近的语法不正确),c#,asp.net,C#,Asp.net,我收到的错误是“user”附近的语法不正确-但是,我看不出我的代码有任何错误-有人有什么想法吗 protected void Page_Load(object sender, EventArgs e) { string db = ""; db = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString; SqlConnection con = new SqlConnection(db)

我收到的错误是“user”附近的语法不正确-但是,我看不出我的代码有任何错误-有人有什么想法吗

protected void Page_Load(object sender, EventArgs e)
{
    string db = "";
    db = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
    SqlConnection con = new SqlConnection(db);
    con.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT * FROM user";
    cmd.Connection = con;
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        Response.Write(dr[0].ToString());
        Response.Write(dr[1].ToString());
        Response.Write(dr[2].ToString());
        Response.Write(dr[3].ToString());
    }

    con.Close();
}
试试这个

"SELECT * FROM [user]";

用户是关键字/保留字。我想这就是问题所在。我总是使用用户而不是用户。尽可能避免使用保留字作为列名、表名等。如果无法避免,请使用保留字。

用户是保留字-您需要将其更改为

"select * from [user]"

这是因为user是一个保留字,大概也是表名

试一试


从[user]

中选择*是否有名为user的表?您的连接字符串正确吗?这是因为用户是保留字,并且可能是表名吗?尝试从[用户]中选择*