【问题标题】:How to Solve Sql Command error invalid syntax near the keyword 'user' [duplicate]如何解决关键字'user'附近的Sql Command错误无效语法[重复]
【发布时间】:2016-09-09 08:21:18
【问题描述】:

我正在尝试连接并从表 dbo.user 中获取数据,但由于关键字“dbo.user”附近的语法无效而出现错误 下面是我的代码

private DataTable GetData()
{

    string constr = ConfigurationManager.ConnectionStrings["PoojaDBConnection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {

        using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.user"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

我在sda.Fill(dt); 声明中遇到了问题

【问题讨论】:

    标签: c# asp.net sql-server


    【解决方案1】:

    user 是 SQL 保留字。将查询更改为:

    "SELECT * FROM dbo.[user]"
    

    始终首选使用[] 装饰架构/表/列名称以避免这些错误。

    Docs:

    ODBC 保留关键字保留用于 ODBC 函数调用。这些话不限制最低SQL 语法;但是,为了确保与支持的驱动程序兼容 核心 SQL 语法,应用程序应避免使用这些关键字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多