string tablePath = @"D:\data\code.dbf"; // 必須含完整路徑
string connStr = $@"Driver={{Microsoft Visual FoxPro Driver}};SourceType=DBF;SourceDB={tablePath};Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO";
using (OdbcConnection conn = new OdbcConnection(connStr))
{
conn.Open();
// 更新 (表名需含路徑)
using (OdbcCommand cmd = conn.CreateCommand())
{
cmd.CommandText = $"UPDATE '{tablePath}' SET other='2', rate=1.014";
cmd.ExecuteNonQuery();
}
// 查詢繫結 GridView
string sql = $"SELECT * FROM '{tablePath}'";
using (OdbcDataAdapter da = new OdbcDataAdapter(sql, conn))
{
DataTable dt = new DataTable();
da.Fill(dt);
gridView1.DataSource = dt.DefaultView;
gridView1.DataBind();
}
}
vfpodbc.dll 為 1.0 版,**必須替換為 6.1.8630.1** (通常在 C:\WINDOWS\system32),叢集環境所有節點版本需一致。CodePage=936 或讀取後手動轉碼。DELETED=NO 讀取含刪除標記記錄,YES 則自動過濾。Prefer 32-bit)。string oleConn = $@"Provider=VFPOLEDB.1;Data Source={Path.GetDirectoryName(tablePath)};Extended Properties=dBASE IV;";
using (OleDbConnection conn = new OleDbConnection(oleConn)) { ... }
VFPOLEDB 支援更完整的 VFP 功能 (如資料庫容器 .dbc),但同樣需注意位元數相容。
ODBC + VFP Driver 是存取 DBF 最通用路徑,**驅動版本一致性** 是部署成敗關鍵。新專案建議遷移至 SQLite / SQL Server LocalDB 等現代嵌入式資料庫。