使用 oledb 更新 dbf 文件中以数字开头的列名

Update a column name that starts with a number in a dbf-file with oledb

我正在尝试更新 dbf 文件中名为 value 的列。由于值是保留字,我需要将表名添加到查询中。所以这有效:

UPDATE "C:\TEMP\TEST_PARAM.DBF" SET TEST_PARAM.value='new value' WHERE Id='Some ID';

但现在我遇到的问题是,我的许多 dbf 文件的文件名都以数字开头,而后面的内容不起作用:

UPDATE "C:\TEMP[=11=]16_PARAM.DBF" SET 0016_PARAM.value='new value' WHERE Id='Some ID';

我试过用单引号、双引号、[... 将 table_name 括起来,但没有任何效果。还有什么我可以尝试的吗?

你没有说你用什么语言来做这件事,但我们在这里使用 C#。同样的方法应该适用于任何语言。

您需要在别名下打开 DBF,并且您需要能够通过 OLEDB 发送多个命令。

这应该有效。

* -- Open the dbf in the first available work area under the alias 'param'. 
* -- Now you don't have to worry about the zeroes.
OleDbCommand myCommand = new OleDbCommand(@"execscript([use 'C:\TEMP[=10=]16_PARAM.DBF' in 0 alias param])", myConnection);

var result = myExecuteNonQuery();

* -- Now do the update, referring to the alias, not the DBF filename.
myCommand = new OleDbCommand(@"execscript([update param set param.value='new' where id='some id'])", myConnection);

result = myCommand.ExecuteNonQuery();

关于方括号,Visual FoxPro 有三个默认的字符串定界符,即通常的单引号和双引号,也有方括号。

所以我们对 C# 字符串使用双引号。我们通过 ExecScript 运行 的 Visual Foxpro 命令也需要引号,围绕 'new' 和 'some id',因此使用单引号。但是我们需要将该命令作为字符串传递给 Execscript,以便该字符串使用方括号。