淘先锋技术网

首页 1 2 3 4 5 6 7

DELPHI 将XML文件通过ClentDataSet存入数据库中

  if dlgOpen1.Execute then
    XMLDocument1.LoadFromFile(dlgOpen1.FileName);
  rootNode := XMLDocument1.DocumentElement;
  nodelist := rootNode.ChildNodes;  
crsZipCode.Open;
  try
    for I := 0 to nodelist.Count - 1 do
    begin
      crsZipCode.Append;
      for j := 0 to nodelist[i].ChildNodes.Count - 1 do
        crsZipCode.FieldValues[nodelist[i].ChildNodes[j].NodeName] :=
        nodelist[i].ChildNodes[j].NodeValue;
      crsZipCode.Post;
    end;
  except
    on e:Exception do
      DialogWarning(e.Message);
  end;
   crsZipCode.ApplyUpdates;
这种效率理论上来说是比从数据库中逐条插入效率高很多的。

  rootNode := XMLDocument1.DocumentElement;
  nodelist := rootNode.ChildNodes;
  for i:= 0 to rootNode.ChildNodes.Count do
  begin
    crsZipCode.CommandText := Format(sSql,
      [quotedstr(rootNode.ChildNodes[i].ChildNodes[0].NodeValue),
       quotedstr(rootNode.ChildNodes[i].ChildNodes[1].NodeValue),
       quotedstr(rootNode.ChildNodes[i].ChildNodes[2].NodeValue),
       quotedstr(rootNode.ChildNodes[i].ChildNodes[3].NodeValue)]);
    try
      crsZipCode.Execute;
    except
      DialogWarning('插入数据有误');
    end;
  end;