关键字“User”附近的语法不正确 [英] Incorrect syntax near the keyword 'User'

查看:285
本文介绍了关键字“User”附近的语法不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是:


System.Data.dll中发生类型为System.Data.SqlClient.SqlException的未处理异常

其他信息:关键字User附近的语法不正确。

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near the keyword '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();
        }

    }
}


推荐答案

用户是SQL Server中的一个内置函数。你需要用方括号括起来: [User] 。这适用于与关键字,保留字或内置名字碰撞的所有表名和其他用户定义的名称,所以我怀疑你需要写 [Order] ,因为 ORDER 是一个SQL关键字。

User is a built-in function in SQL Server. You need to surround the name with square brackets: [User]. This goes for all table names and other user-defined names that happen to collide with keywords, reserved words or built-in names, so I suspect you will need to write [Order] as well, since ORDER is an SQL keyword.

这篇关于关键字“User”附近的语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆