关键字“User”附近的语法不正确。

3
错误为:

发生了类型为'System.Data.SqlClient.SqlException'的未处理异常,详细信息: 关键字“User”附近的语法不正确。

代码为:
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using System.Data.SqlClient;  

namespace WindowsFormsApplication1  
{  
    public partial class Form1 : Form  
    {  
        SqlConnection conn;  
        SqlDataAdapter GameDA;  
        SqlDataAdapter DetailDA;  
        DataSet DetailDS;  
        SqlCommandBuilder cmdBuilder;  
        SqlDataAdapter UserDA;  
        SqlDataAdapter AdministratorDA;  
        SqlDataAdapter OrderDA;  
        DataSet OrderDS;  
        SqlCommandBuilder cmdBuilder2;  

        public Form1()
        {
            InitializeComponent();
            conn = new SqlConnection("Data Source=HOME-AC284121FE\\SQLEXPRESS;Initial Catalog=GameShop;Integrated Security=SSPI;");
            SqlCommand command1 = new SqlCommand("SELECT * FROM Game", conn);
            GameDA = new SqlDataAdapter(command1);
            SqlCommand command2 = new SqlCommand("SELECT * FROM Detail WHERE GameID = @GameID", conn);
            command2.Parameters.Add(new SqlParameter("@GameID", SqlDbType.Int));
            DetailDA = new SqlDataAdapter(command2);
            SqlCommand command3 = new SqlCommand("SELECT * FROM Administrator", conn);
            AdministratorDA = new SqlDataAdapter(command3);
            SqlCommand command4 = new SqlCommand("SELECT * FROM User", conn);
            UserDA = new SqlDataAdapter(command4);
            SqlCommand command5 = new SqlCommand("SELECT * FROM Order WHERE UserID = @UserID", conn);
            command5.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int));
            OrderDA = new SqlDataAdapter(command5);
            cmdBuilder2 = new SqlCommandBuilder(OrderDA);
            cmdBuilder = new SqlCommandBuilder(DetailDA);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DetailDS = new DataSet();
            OrderDS = new DataSet();

            GameDA.Fill(DetailDS, "Game");
            **UserDA.Fill(OrderDS, "User"); // <-- Error**
            AdministratorDA.Fill(OrderDS, "Administrator");

            comboBoxGame.DisplayMember = "Name";
            comboBoxGame.ValueMember = "GameID";
            comboBoxGame.DataSource = DetailDS.Tables["Game"];

            dataGridView.DataSource = DetailDS.Tables["Detail"];
            dataGridView.Columns["GameID"].Visible = false;
            dataGridView.Columns["DetailID"].Visible = false;


        }

        private void comboBoxGame_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxGame.SelectedValue != null)
                if (DetailDS.Tables.Contains("Detail"))
                {
                    DetailDS.Tables["Detail"].Clear();
                }
            DetailDA.SelectCommand.Parameters[0].Value = comboBoxGame.SelectedValue;
            DetailDA.Fill(DetailDS, "Detail");
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}

1
你好,欢迎来到StackOverflow!在您输入问题时,右侧有一个“如何格式化”的框,其中包含快速格式化提示(使用四个空格缩进代码,或使用编辑工具栏上的{}按钮)。还有一个位于问题区域上方的**[?]**链接,用于获取更多详细信息,并在其下方提供预览区域以检查结果。(这只是为了下一次。我本想帮你修复它,因为你是新手,我们喜欢帮助新手,但@Rup比我先完成了。) - T.J. Crowder
2个回答

17

"User"是SQL Server中的保留字,因此您必须使用限定符标识符来引用您的表。尝试:

SqlCommand command4 = new SqlCommand("SELECT * FROM [User]", conn);

而是将表重命名为不被保留的名称。

(我还强烈建议您将数据访问从UI代码中分离出来,正确处理连接等等...但这是另一回事。)


15

User 是 SQL Server 中的内置函数。您需要使用方括号将名称括起来:[User]。对于所有表名和其他与关键字、保留字或内置名称发生冲突的用户定义名称,都需要这样做,因此我怀疑您还需要编写 [Order],因为 ORDER 是 SQL 关键字。


1
被Jon Skeet打败了,只差2秒... :-( - Aasmund Eldhuset

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接