关于Access的数据导出和导入(100分)

  • 主题发起人 主题发起人 ago_go
  • 开始时间 开始时间
A

ago_go

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在将ACCESS数据库中的表中相关数据导出为文本格式或其他格式的文件,
再在另一台电脑的ACCESS表中导入。(通过DELPHI语句)
选择数据SQL语句为:
select FName,FGuestName,FGuestTel,FGuestAdd,FSendTime
from User_table
where endtime='2002-03-24'
急求帮助,100分
 
procedure TForm1.N5Click(Sender: TObject);
var
str:string;
filename:textfile;
i,j,n:integer;
p:string;
begin
if SaveDialog2.Execute then
begin
str:=SaveDialog2.FileName;
assignfile(filename,str);
if fileexists(str) then DeleteFile(SaveDialog1.FileName);
Screen.Cursor:=crHourGlass;
rewrite(filename);
p:='|';
str:='';
n:=0;//query字段N序号
Query1.First;
for n:=0 to Query1.FieldCount -1 do
begin
str:=str+Query1.fields[n].DisplayLabel+p;
end;
writeln(filename,str);
n:=0;//query字段N序号
str:='';
while not Query1.Eof do begin
for n:=0 to Query1.FieldCount -1 do
begin
str:=str+Query1.fields[n].AsString+p;
end;
writeln(filename,str);
str:='';
Query1.Next;
end;
Screen.Cursor:=crdefault;
Application.MessageBox('操作在没有警告的情况下正常结束!','完成',mb_ok+mb_iconinformation);
closefile(filename);
end;
end;
 
导入:

procedure RegulateStr(aString:String;Sepchar:String;Isfirst:boolean);
//aString 为取出的字符串,Sepchar为分隔符.
var
Num:Integer;
MyStr,qryStr:String;
begin
Num:=0;
//i:=length(aString);
while length(astring)<>0 do begin
mystr:=copy(astring,1,pos(sepchar,astring)-1);//截取子符串
delete(astring,1,pos(sepchar,astring)); //删除子符串
if mystr=''then mystr:='1.00'; //防止空字符
if Isfirst then begin//如果是第一条,则特殊处理。
Form1.Query1.fields[Num].DisplayLabel:=mystr;
end else
begin
Form1.Query1.Fields[Num].AsString:=mystr;
end;
Num:=Num+1;

end;
end;

procedure TForm1.N7Click(Sender: TObject);
var
filestr:Textfile;
recoStr:string;
begin
OpenDialog1.Filter:='ASCII Files(*.TXT)|*.txt';
if OpenDialog1.Execute then
begin
Screen.Cursor:=crHourGlass;
assignfile(filestr,opendialog1.filename);
Reset(filestr);
Readln(filestr,recostr);//读出一行字符串。
RegulateStr(recostr,'|',true);
while not eof(filestr) do
begin
Query1.Insert;
Readln(filestr,recostr);
RegulateStr(recostr,'|',false);//循环取出所有子符串。
Query1.Next;
//读下一行。
end;
end;
Screen.Cursor:=crdefault;
Application.MessageBox('操作在没有警告的情况下正常结束!','完成',mb_ok+mb_iconinformation);
closefile(filestr);
end;

end.
 
应该是类似下面的语句

SELECT *
into Targettable
IN "" [Text; DATABASE=C:/path/;]
from sourcetable
 
不懂你的意思,可以直接倒入的,为什么要麻烦DELPHI呢
 
ADO好象有 savetofile 和 loadfromfile 的。
你试试看。。。。
 
wzca说的 对吗
 
来看HELP。呵呵
TCustomADODataSet.SaveToFile
-----------------------------
TCustomADODataSet
================================
Saves a recordset to a file.
procedure SaveToFile(const FileName: String = ''; Format: TPersistFormat = pfADTG);
Description
Call SaveToFile to save the current recordset to a file. If the destination file already exists, it is overwritten.
FileName is a string containing the name of the destination file. This file remains open from the first call to SaveToFile until the dataset is closed. This file can be read by other applications while it is open, but they cannot write to the file.
Format specifies the file format for the saved recordset. By default, Format is pfADTG (Advanced Data Tablegram format).
Note: Microsoft recommends using a client-side cursor (the dataset is opened with a CursorLocation property value of clUseClient). This way, if the provider used does not support saving the recordset the client cursor will provide the necessary functionality.
 
TCustomADODataSet.LoadFromFile
TCustomADODataSet
===================================
Loads a recordset from a file.
procedure LoadFromFile(const FileName: WideString);
Description
Call LoadFromFile to load the recordset for the calling ADO dataset component from a file. If the loading operation fails, the current recordset is neutralized (set to nil), the dataset component remains inactive, and an EOleException exception is raised. If the attempt to load data from a file is successful, the ADO dataset component is automatically activated and the data made available.
FileName is a string containing the name of the file.
LoadFromFile closes the dataset component before loading the recordset from the file specified in FileName.
 
wzca的就枵以了.
 
各位大侠,兄弟我很菜,能不能把工程文件与原文件等全部发在我的信箱里,agou@163.net
100 分一分不省,谢谢
 
动态创建文本文件,然后将select出的记录插入文件中,一条记录一行,字段间
用分隔符分隔。
导入时,按行读取文本中的记录根据分隔符将每个字段付给一数组,然后将其插入
数据库。
最好作一函数判断分隔符。
 
SavetoFile 不可以吗?
 
后退
顶部