使用 C# 打开旧的 VB6 随机访问文件
Open old VB6 Random Access File using C#
我正在尝试打开使用随机访问创建的旧 VB6 文件。
使用的类型如下:
Type CDB
dateCreated As Date
lastModified As Date
companyName As String * 30
ownerName As String * 30
contactName As String * 30
addresss As String * 100
tel As String * 75
vat As String * 8
BRegd As String * 9
End Type
访问如下:
Dim CDB As CDB
Open "CLIENTS.DAT" For Random As #1 Len = Len(CDB)
Lastrec = LOF(1) / Len(CDB)
For rec = 1 To Lastrec
Get #1, rec, CDB
txtDateCreated.Text = Format(CDB.dateCreated, "dd/mm/yyyy")
txtLastModified.Text = Format(CDB.lastModified, "dd/mm/yyyy")
txtCompanyName.Text = Trim(CDB.companyName)
... and so on
Next
现在我想使用 C# 打开此文件并将所有数据导入 SQL 数据表中。
谁能帮我打开这个使用 CDB 类型作为结构的文件?
要使用我的示例,您已经为 Microsoft.VisualBasic.Filesystem 程序集创建了一个别名:
Imports VB6FileSystem = Microsoft.VisualBasic.FileSystem
Imports VB = Microsoft.VisualBasic
然后在您的代码中:
// make sure the read buffer is big enough
string testReadData = "".PadRight(128);
int filenumber = VB6FileSystem.FreeFile();
VB6FileSystem.FileOpen(filenumber, @"c:\temp\test.dat", VB.OpenMode.Random, RecordLength: 128);
// Write some test data ....
VB6FileSystem.FilePut(filenumber, "Testdaten 1", 1, true);
VB6FileSystem.FilePut(filenumber, "Testdaten 4", 4, true);
VB6FileSystem.FilePut(filenumber, "Testdaten 14", 14, true);
// Read some data ...
VB6FileSystem.FileGet(filenumber, ref testReadData, 14, true);
VB6FileSystem.FileClose(filenumber);
当然你必须分析旧的记录结构(vb6 知道 "fixed length strings",而 C# 并不真正知道它们......)以及数据在文件中的表示方式。也许你应该在 c# 中读取字节数组来处理二进制数据(如日期、数字......)"by hand".
我没有尝试的是使用字节数组引用变量的 FileGetObject 方法。放心完成任务吧。
我正在尝试打开使用随机访问创建的旧 VB6 文件。
使用的类型如下:
Type CDB
dateCreated As Date
lastModified As Date
companyName As String * 30
ownerName As String * 30
contactName As String * 30
addresss As String * 100
tel As String * 75
vat As String * 8
BRegd As String * 9
End Type
访问如下:
Dim CDB As CDB
Open "CLIENTS.DAT" For Random As #1 Len = Len(CDB)
Lastrec = LOF(1) / Len(CDB)
For rec = 1 To Lastrec
Get #1, rec, CDB
txtDateCreated.Text = Format(CDB.dateCreated, "dd/mm/yyyy")
txtLastModified.Text = Format(CDB.lastModified, "dd/mm/yyyy")
txtCompanyName.Text = Trim(CDB.companyName)
... and so on
Next
现在我想使用 C# 打开此文件并将所有数据导入 SQL 数据表中。 谁能帮我打开这个使用 CDB 类型作为结构的文件?
要使用我的示例,您已经为 Microsoft.VisualBasic.Filesystem 程序集创建了一个别名:
Imports VB6FileSystem = Microsoft.VisualBasic.FileSystem
Imports VB = Microsoft.VisualBasic
然后在您的代码中:
// make sure the read buffer is big enough
string testReadData = "".PadRight(128);
int filenumber = VB6FileSystem.FreeFile();
VB6FileSystem.FileOpen(filenumber, @"c:\temp\test.dat", VB.OpenMode.Random, RecordLength: 128);
// Write some test data ....
VB6FileSystem.FilePut(filenumber, "Testdaten 1", 1, true);
VB6FileSystem.FilePut(filenumber, "Testdaten 4", 4, true);
VB6FileSystem.FilePut(filenumber, "Testdaten 14", 14, true);
// Read some data ...
VB6FileSystem.FileGet(filenumber, ref testReadData, 14, true);
VB6FileSystem.FileClose(filenumber);
当然你必须分析旧的记录结构(vb6 知道 "fixed length strings",而 C# 并不真正知道它们......)以及数据在文件中的表示方式。也许你应该在 c# 中读取字节数组来处理二进制数据(如日期、数字......)"by hand".
我没有尝试的是使用字节数组引用变量的 FileGetObject 方法。放心完成任务吧。