各位专家:
  大家好!我是一个新手,才下了“C#Web应用程序入门经典”源代码,并附加了数据库FriendsData数据库。今天编了代码读表User中的数据,总是提示错误“关键字 'user' 附近有语法错误”,于是拿了“C#入门经典”中一个例子,另外的数据运行正常,于是将数据库名、表名和显示的字段名相应的修改,执行到“SqlDataReader thisreader = thiscommand.ExecuteReader();”处仍出现上述错误提示,但别的数据表均能正常按要求显示。用BindingSource绑定时显示正常,不知是什么原因,是不是我的代码有错,请各位高手指教。谢谢!
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace CAPI_testconnect
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection thisconnection = new SqlConnection("data source=PRIVATE-CREATOR\\SQL2005;Initial Catalog=FriendsData;User ID=sa;Password=123456");
            thisconnection.Open();            SqlCommand thiscommand = thisconnection.CreateCommand();            thiscommand.CommandText = "select * from user";            SqlDataReader thisreader = thiscommand.ExecuteReader();//此处出现错误提示:“关键字 'user' 附近有语法错误”,其他各表都可以按要求正常显示,不知为什么?            while (thisreader.Read())
            {
                Console.WriteLine("\t{0}\t{1}", thisreader["firstname"], thisreader["lastname"]);
            }            thisreader.Close();
            thisconnection.Close();                          Console.Write("this program finished ,press Enter /Return to continue");
                Console.Read();
            
        }
    }
}

解决方案 »

  1.   

    user 加上 []   等于 [user]
      

  2.   

     thiscommand.CommandText = "select * from [user]"; 
      

  3.   


    select * from user  --看两个的区别
    select * from [User]执行Sql语句或者是存储的时候报错 多半是你的Sql语句的问题
    user是关键在  加上界定符号 
      

  4.   

    对于SQL语句中的关键词作为数据库里的表名或列名等,都要加上方括号
      

  5.   

    你应该用的是vs2003  吧  2005应该可以 
    但是 建议在建立表的时候 不要使用关键字 养成好的习惯。。user  换成UesrInfo  什么的