如何将 DataTable 保存到 .DBF?
我一直在开发一个程序来读取 dbf 文件,处理数据,然后将其保存回 dbf。 我遇到的问题具体与写作部分有关。
private const string constring = "Driver={Microsoft dBASE Driver (*.dbf)};"
+ "SourceType=DBF;"
+ "DriverID=277;"
+ "Data Source=¿;"
+ "Extended Properties=dBASE IV;";
private const string qrystring = "SELECT * FROM [¿]";
public static DataTable loadDBF(string location)
{
string filename = ConvertLongPathToShort(Path.GetFileName(location));
DataTable table = new DataTable();
using(OdbcConnection conn = new OdbcConnection(RTN(constring, filename)))
{
conn.Open();
table.Load(new OdbcCommand(RTN(qrystring, filename), conn).ExecuteReader());
conn.Close();
}
return table;
}
private static string RTN(string stmt, string tablename)
{ return stmt.Replace("¿", tablename); }
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(
String path, // input string
StringBuilder shortPath, // output string
Int32 shortPathLength); // StringBuilder.Capacity
public static string ConvertLongPathToShort(string longPathName)
{
StringBuilder shortNameBuffer;
int size;
shortNameBuffer = new StringBuilder();
size = GetShortPathName(longPathName, shortNameBuffer, shortNameBuffer.Capacity);
if (size >= shortNameBuffer.Capacity)
{
shortNameBuffer.Capacity = size + 1;
GetShortPathName(longPathName, shortNameBuffer, shortNameBuffer.Capacity);
}
return shortNameBuffer.ToString();
}
这就是我正在处理的事情。 我尝试了多种方法来写入新文件,但没有一种有效。 老实说,虽然通常我会是形式和功能的倡导者,但我只是想让该死的东西发挥作用,这个应用程序应该做一件非常具体的事情,它不会模拟天气。
-=# 编辑 #=-
由于时间压力,我已经停止使用该应用程序,但在废弃它之前,我意识到我正在使用的 dbf 的特定格式没有主键信息。 这当然意味着我必须将数据读取到 DataTable 中,对其进行处理,然后擦除 dbf 中的所有记录并从头开始插入所有内容。 管它呢。
I've been working on a program to read a dbf file, mess around with the data, and save it back to dbf. The problem that I am having is specifically to do with the writing portion.
private const string constring = "Driver={Microsoft dBASE Driver (*.dbf)};"
+ "SourceType=DBF;"
+ "DriverID=277;"
+ "Data Source=¿;"
+ "Extended Properties=dBASE IV;";
private const string qrystring = "SELECT * FROM [¿]";
public static DataTable loadDBF(string location)
{
string filename = ConvertLongPathToShort(Path.GetFileName(location));
DataTable table = new DataTable();
using(OdbcConnection conn = new OdbcConnection(RTN(constring, filename)))
{
conn.Open();
table.Load(new OdbcCommand(RTN(qrystring, filename), conn).ExecuteReader());
conn.Close();
}
return table;
}
private static string RTN(string stmt, string tablename)
{ return stmt.Replace("¿", tablename); }
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(
String path, // input string
StringBuilder shortPath, // output string
Int32 shortPathLength); // StringBuilder.Capacity
public static string ConvertLongPathToShort(string longPathName)
{
StringBuilder shortNameBuffer;
int size;
shortNameBuffer = new StringBuilder();
size = GetShortPathName(longPathName, shortNameBuffer, shortNameBuffer.Capacity);
if (size >= shortNameBuffer.Capacity)
{
shortNameBuffer.Capacity = size + 1;
GetShortPathName(longPathName, shortNameBuffer, shortNameBuffer.Capacity);
}
return shortNameBuffer.ToString();
}
This is what I'm working with. I've tried a number of methods to write a new file, none of them productive. To be honest, while normally I would be an advocate of form and function, I just want the damn thing to work, this app is supposed to do one very specific thing, it's not going to simulate weather.
-=# Edit #=-
I've since discontinued the app due to time pressure, but before I scrapped it I realised that the particular format of dbf I was working with had no primary key information. This of course meant that I had to essentially read the data out to DataTable, mess with it, then wipe all the records in the dbf and insert everything from scratch.
Screw that for a lark.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于将来来这里的人:我今天写了这篇文章,效果很好。 文件名不带扩展名 (.dbf)。 路径(用于连接)只是目录路径(无文件)。 您可以将数据表添加到数据集并将其传入。此外,我的一些数据类型是 FoxPro 数据类型,可能不与所有 DBF 文件兼容。 希望这可以帮助。
For people coming here in the future: I wrote this today and it works well. The filename is without the extension (.dbf). The path (used for connection) is the directory path only (no file). You can add your datatable to a dataset and pass it in. Also, some of my datatypes are foxpro data types and may not be compatible with all DBF files. Hope this helps.
您正在使用哪种 dbf 文件? (有几个,例如 dBase、FoxPro 等不是 100% 兼容。)我已经让它与 C# 中的 Microsoft Visual FoxPro OleDB Provider 一起使用,您可以尝试一下,而不是使用 dBase ODBC 驱动程序。
What kind of dbf file are you working with? (There are several, e.g. dBase, FoxPro etc that are not 100% compatible.) I have gotten this to work with the Microsoft Visual FoxPro OleDB Provider from C#, you might give that a shot instead of using the dBase ODBC driver.
使用 ADO.Net 读取和写入 dbf 文件非常慢,因此我建议您使用替代方法。
一种选择是使用旧的 DAO 3.6 库。 这要快得多并且兼容,但依赖于 com 对象才能工作。
更好的方法是使用开源 DBFExporter 组件。 它可能需要一些代码来设置(您需要一个具有描述记录集的属性的类,并且这些属性必须设置某些属性),但之后它就可以很好地工作。 它使用起来很快,但它不读取 dbf 文件。 该组件是 LGPL 下的许可证,因此您应该能够在商业代码中使用它。
Using ADO.Net to read and write dbf files turns out to be really slow so I would suggest you use an alternative approach.
One option would be to use the old DAO 3.6 library. This is much faster and just as compatible, but depends on a com object to work.
A better approach would be to use the open source DBFExporter component. It may require some code to set up (you need a class with properties that describe your recordset and the properties must have certain attributes set) but after that it works really well. It is fast to use but it doesn't read dbf files. The component is licences under the LGPL so you should be able to use it in commercial code.