私は、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 内のすべてのレコードを消去し、すべてを最初から挿入する必要があることを意味していました。ひばりのためにそれをねじ込みます。