【发布时间】:2011-12-20 08:26:55
【问题描述】:
我有一个场景,我必须从 sql 表中导出大约 500,000 条记录的数据,以便在 Delphi 应用程序中使用。数据将被加载到打包记录中。有没有一种方法可以让我使用 BCP 写入数据文件,类似于将记录写入文件。
到目前为止,我正在使用此伪代码加载数据。
// Assign the data file generated from BCP to the TextFile object.
AssignFile(losDataFile, loslFileName);
Reset(losDataFile);
while not EOD(losDataFile) do
begin
// Read from the data file until we encounter the End of File
ReadLn(losDataFile, loslDataString);
// Use the string list comma text to strip the fields
loclTempSlist.CommaText := loslDataString;
// Load the record from the items of the string list.
DummyRec.Name := loclTempSList[0];
DummyRec.Mapped = loclTempSList[1] = 'Y';
end;
为方便起见,我在下面列出了 Dummy rec 的类型
TDummyRec = packed record
Name : string[255];
Mapped : Boolean;
end;
所以,我的问题是,除了将数据导出到文本文件之外,是否可以将数据导出为二进制文件,以便我可以直接使用记录类型从文件中读取?
喜欢
loclFileStream := TFileStream.Create('xxxxxx.dat', fmOpenRead or fmShareDenyNone);
while loclFileStream.Position < loclFileStream.Size do
begin
// Read from the binary file
loclFileStream.Read(losDummyData, SizeOf(TDummyRec));
//- -------- Do wat ever i want.
end;
我没有太多使用 BCP 的经验。请帮我解决这个问题。
谢谢 终结者...
【问题讨论】:
-
只是一个提示,不是回答:TFileStream.
-
为什么不用Delphi直接访问SQL Server呢?
-
Jens.. 我提供的示例仅适用于 2 列,但在实际情况下,我有 140 列数据要加载。我尝试使用 TADOQuery 加载它们,但应用程序的 2GB 内存已用完。所以,我想到了这个替代方案......
-
@RahulW ADO 能够检索任意大小的数据,每行一行。我认为您正在将这些数据映射到内存中。