【问题标题】:Query a table that has spaces in its name查询名称中包含空格的表
【发布时间】:2011-06-27 20:00:59
【问题描述】:

我有一个情况,我有一个名为 Gas Flow Rates 的访问表,我想添加记录。当我尝试对类似的表 Common Station 运行插入查询时,我收到以下错误:

"error hy000: 语法错误,在查询不完整的查询子句中"

代码是:

using System;
using System.Data.Odbc;

class MainClass
{
static void Main(string[] args)
{
    string connectionString = "Dsn=Gas_meter";
    string sqlins = "";
    OdbcConnection conn = new OdbcConnection(connectionString);

    OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);
    conn.Open();

    try
    {
       cmdnon.CommandText = "INSERT INTO 'Common station' ( S1Flow, S2Flow, S3Flow, S4Flow) VALUES (9999,999, 999, 999)";
        //Once the above line works replace it with cmdnon.CommandText= "INSERT INTO Gas Flow Rates ( S1Flow, S2Flow, S3Flow, S4Flow) VALUES (9999,999, 999, 999)"
        int rowsAffected = cmdnon.ExecuteNonQuery();
        Console.WriteLine(rowsAffected);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        conn.Close();
    }
}
}

我该如何克服这个错误?

【问题讨论】:

    标签: c# ms-access-2007 tablename


    【解决方案1】:

    用方括号将隔开的项目括起来:

    [Common station]
    

    那就给设计数据库的人一巴掌吧。

    【讨论】:

    • 谢谢,如果他不住在亚利桑那州,我绝对会的。我想我会给他发一个 Chappelle 表演 Rick James 短剧的片段,Rick James 告诉他五个手指对脸上的笑话说了些什么。 LMBO
    • 我遇到了一个使用关键字作为列名的人的问题。相同的解决方案(即 [] - 也许是耳光?)。我们不这样做是有原因的(可维护性这个词有什么意义吗?)。
    • 不是真的,不能从 Gas_Flow_Rates 或 GasFlowRates 派生出来的重要性不高。据我所知,这正是他创建表格的方式。
    • 我笑得很厉害。
    【解决方案2】:

    SELECT * FROM [My Crazy Table With Spaces and Other Chars!]

    使用方括号“引用”表格和字段名称。

    【讨论】:

      【解决方案3】:
        cmdnon.CommandText = "INSERT INTO '[Common station]' ( S1Flow, S2Flow, S3Flow, S4Flow) VALUES (9999,999, 999, 999)";
          //Once the above line works replace it with cmdnon.CommandText= "INSERT INTO Gas Flow Rates ( S1Flow, S2Flow, S3Flow, S4Flow) VALUES (9999,999, 999, 999)"
      

      【讨论】:

        【解决方案4】:

        我知道的聚会迟到了,但我刚刚在这里解决了我自己的问题...... 使用 ODBC 连接到 SQL Db 在 access 2007 中播放。

        表名是 Employee_Appointment Extra Detail Custom 选择的语法如下 SQlRecordSet.Open "Select * from [Employee].[Appointment Extra Detail Custom]", Conn, adOpenStatic, adLockOptimistic

        希望这可以为其他人节省几个小时的游戏时间!

        【讨论】:

          猜你喜欢
          • 2015-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-30
          • 1970-01-01
          • 2023-01-07
          • 1970-01-01
          相关资源
          最近更新 更多