10

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

“错误 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();
    }
}
}

我该如何克服这个错误?

4

4 回答 4

52

用方括号括住隔开的项目:

[Common station]

然后给设计数据库的人一巴掌。

于 2011-06-27T20:03:42.257 回答
6

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

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

于 2011-06-27T20:03:26.783 回答
4
  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)"
于 2011-06-27T20:03:11.570 回答
2

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

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

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

于 2017-10-27T15:35:23.680 回答