adoQuery 的LoadFromFile可以直接把XML文件显示在表中,如果想写入数据据库,只能自已一条条的插入数据库。下面是我曾经写的一个将一个XML文件导入到正在打开的数据表中,看看对你是否有用?
procedure TFrm_table_edit.btn2Click(Sender: TObject);
var
i:integer;
blExecSucc:Boolean ;
strSQLsoure,strTableName,strFileName:string;
myDataSet:Tdataset ;
function myExecSQL(strSQLs:string):Boolean ;
begin
with (myDataSet as TADOQuery ) do
begin
Active := False;
SQL.Clear ;
SQL.Add(strsqls);
try
ExecSQL ;
Result := True;
except
Result := False ;
end;
end;
end;
function insertTable():Boolean ;
var
x,j:integer;
begin
if myDataSet.Active then j:= myDataSet.FieldCount
else begin
Result := False;
Exit;
end;
with dm.adoqryXML do
begin
First ;
while not(DM.adoqryXML.Eof ) do
begin
myDataSet.Insert ;
for x:=0 to j -1 do
begin
myDataSet.Fields[x].AsVariant := Fields[x].AsVariant ;
end;
try
myDataSet.Post ;
except
Result := False;
Exit;
end;
Next ;
end;
Result := True;
end;
end;
begin
if dlgOpen1.Execute then
begin
strFileName:= dlgOpen1.FileName ;
//ShowMessage('filename='+ strFilename );
DM.adoqryXML.LoadFromFile(strFileName );
end
else begin
Exit;
end;
myDataSet := TDataSet.Create(Self );
myDataSet := dbg1.DataSource.DataSet ;
strSQLsoure := (dbg1.DataSource.DataSet as TADOQuery ).SQL[0];
strTableName := getTableName(strSQLsoure );
if (Application.MessageBox(PChar('确定要将文件 '+ strFileName +' 导入至'
+'数据表 '+ strTableName +' 中吗?'), PChar(Caption),
MB_YESNO + MB_ICONINFORMATION) = IDYES ) then
try
dbg1.DataSource.DataSet.DisableControls ;
with (myDataSet as TADOQuery ) do
begin
myExecSQL('delete from '+ strTableName );
Active := False;
SQL.Clear ;
SQL.Add('select * from '+ strTableName );
Active := True ;
blExecSucc := insertTable ;
SQL.Clear ;
SQL.Add(strSQLsoure );
Active := True;
if blExecSucc then
ShowMessage(strTableName +'表导入成功')
else ShowMessage(strTableName +'表导入失败');
end;
finally
dbg1.DataSource.DataSet.EnableControls ;
end;
end;