如何向 .DBF 文件添加列?

发布于 2024-12-23 11:42:29 字数 784 浏览 1 评论 0原文

我遇到的问题之一是,我认为该表没有名称...它只是一个 .dbf 文件

所以我一直在尝试这样做:

public void SQLAlter(string dbffile, string ColumnName )
{
   //dbffile is "C:\MAPS\WASHINGTON\TLG_ROADS_L.DBF"
   //ColumnName is "State"
   if (File.Exists(dbffile))
   {
        System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection();
        conn.ConnectionString = @"DSN=dBase Files";
        conn.Open();
        System.Data.Odbc.OdbcCommand comm = new System.Data.Odbc.OdbcCommand();
        comm.CommandText = "ALTER TABLE " + dbffile + " ADD COLUMN " + ColumnName + " VARCHAR(1024)";
        comm.ExecuteNonQuery();
    }
 }

错误是:

基{System.Data.Common.DbException} = {“错误[42S02] [Microsoft][ODBC dBASE 驱动程序] 找不到表或约束。"}

One of the issues I have is that, I don't think the table has a name... its just a .dbf file

So I've been trying this:

public void SQLAlter(string dbffile, string ColumnName )
{
   //dbffile is "C:\MAPS\WASHINGTON\TLG_ROADS_L.DBF"
   //ColumnName is "State"
   if (File.Exists(dbffile))
   {
        System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection();
        conn.ConnectionString = @"DSN=dBase Files";
        conn.Open();
        System.Data.Odbc.OdbcCommand comm = new System.Data.Odbc.OdbcCommand();
        comm.CommandText = "ALTER TABLE " + dbffile + " ADD COLUMN " + ColumnName + " VARCHAR(1024)";
        comm.ExecuteNonQuery();
    }
 }

The error is :

base {System.Data.Common.DbException} = {"ERROR [42S02]
[Microsoft][ODBC dBASE Driver] Cannot find table or constraint."}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汐鸠 2024-12-30 11:42:29

我相信表名应该是文件名,连接字符串应该指向包含 dbf 文件的文件夹。

var path = Path.GetDirectoryName(dbffile);
var tableName = Path.GetFileName(dbffile);
// ...
conn.ConnectionSTring = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" + path;
comm.CommandText = "ALTER TABLE " + tableName + //...

检查connectionstrings.com:http://connectionstrings.com/dbf-foxpro

I believe the table name is supposed to be the filename, and the connection string should point to the folder containing the dbf file.

var path = Path.GetDirectoryName(dbffile);
var tableName = Path.GetFileName(dbffile);
// ...
conn.ConnectionSTring = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" + path;
comm.CommandText = "ALTER TABLE " + tableName + //...

Check connectionstrings.com: http://connectionstrings.com/dbf-foxpro

陈甜 2024-12-30 11:42:29

这实际上是正确的语法

comm.CommandText = "ALTER TABLE " + dbffile + " ADD COLUMN " + ColumnName + " VARCHAR(1024)";

,但是如果您的文件名超过 8 个字符,它将找不到它。尽管我尝试使用适当长度的文件名,但“包含数据的表不支持该操作”。

各种互联网链接似乎表明必须创建一个新表,并复制所有字段。

This is actually the correct syntax

comm.CommandText = "ALTER TABLE " + dbffile + " ADD COLUMN " + ColumnName + " VARCHAR(1024)";

However if your filename is longer than 8 characters it will not find it. Even though I tried it will an appropriate length file name, the "Operation [is] not supported on a table that contains data."

Various Internet links seem to indicate that one has to create a new table, and copy all the fields over.

祁梦 2024-12-30 11:42:29

尝试另一个提供商。

它与 Visual Foxpro Provider 对我有用
conn.ConnectionString = @"Provider=VFPOLEDB.1; 数据源=Themes.dbf" + @"\;扩展属性=dBase IV";

如果您的计算机上未安装驱动程序,您可以在此处获取:
http://www.microsoft.com/en-us/download /details.aspx?id=14839

Try another Provider.

It worked for me with the Visual Foxpro Provider
conn.ConnectionString = @"Provider=VFPOLEDB.1; Data Source=Themes.dbf" + @"\;Extended Properties=dBase IV";

If the driver is not installed on your machine, you get it here :
http://www.microsoft.com/en-us/download/details.aspx?id=14839

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文