SQL SERVER的IMAGE字段数据导到XML文件中,如何再导回SQL Server(300分)

  • 主题发起人 HunterTeam
  • 开始时间
H

HunterTeam

Unregistered / Unconfirmed
GUEST, unregistred user!
导到XML文件后,字段内容全成了文本,如何才能正确导回到SQL SERVER数据库中?
 
数据库转换成XML<br>procedure DatasetToXML(Dataset: TDataset; FileName: string); <br><br>unit DS2XML; <br><br>interface <br><br>uses <br>&nbsp; Classes, DB; <br><br>procedure DatasetToXML(Dataset: TDataset; FileName: string); <br><br>implementation <br><br>uses <br>&nbsp; SysUtils; <br><br>var <br>&nbsp; SourceBuffer: PChar; <br><br>procedure WriteString(Stream: TFileStream; s: string); <br>begin <br>&nbsp; StrPCopy(SourceBuffer, s); <br>&nbsp; Stream.Write(SourceBuffer[0], StrLen(SourceBuffer)); <br>end; <br><br>procedure WriteFileBegin(Stream: TFileStream; Dataset: TDataset); <br><br>&nbsp; function XMLFieldType(fld: TField): string; <br>&nbsp; begin <br>&nbsp; &nbsp; case fld.DataType of <br>&nbsp; &nbsp; &nbsp; ftString: Result := '"string" WIDTH="' + IntToStr(fld.Size) + '"'; <br>&nbsp; &nbsp; &nbsp; ftSmallint: Result := '"i4"'; //?? <br>&nbsp; &nbsp; &nbsp; ftInteger: Result := '"i4"'; <br>&nbsp; &nbsp; &nbsp; ftWord: Result := '"i4"'; //?? <br>&nbsp; &nbsp; &nbsp; ftBoolean: Result := '"boolean"'; <br>&nbsp; &nbsp; &nbsp; ftAutoInc: Result := '"i4" SUBTYPE="Autoinc"'; <br>&nbsp; &nbsp; &nbsp; ftFloat: Result := '"r8"'; <br>&nbsp; &nbsp; &nbsp; ftCurrency: Result := '"r8" SUBTYPE="Money"'; <br>&nbsp; &nbsp; &nbsp; ftBCD: Result := '"r8"'; //?? <br>&nbsp; &nbsp; &nbsp; ftDate: Result := '"date"'; <br>&nbsp; &nbsp; &nbsp; ftTime: Result := '"time"'; //?? <br>&nbsp; &nbsp; &nbsp; ftDateTime: Result := '"datetime"'; <br>&nbsp; &nbsp; else <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; if fld.Required then <br>&nbsp; &nbsp; &nbsp; Result := Result + ' required="true"'; <br>&nbsp; &nbsp; if fld.Readonly then <br>&nbsp; &nbsp; &nbsp; Result := Result + ' readonly="true"'; <br>&nbsp; end; <br><br>var <br>&nbsp; i: Integer; <br>begin <br>&nbsp; WriteString(Stream, ' &nbsp;' + <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''); <br>&nbsp; WriteString(Stream, ''); <br><br>&nbsp; {write th metadata} <br>&nbsp; with Dataset do <br>&nbsp; &nbsp; for i := 0 to FieldCount-1 do <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; WriteString(Stream, ''); <br>&nbsp; &nbsp; end; <br>&nbsp; WriteString(Stream, ''); <br>&nbsp; WriteString(Stream, ''); <br>&nbsp; WriteString(Stream, ''); <br>end; <br><br>procedure WriteFileEnd(Stream: TFileStream); <br>begin <br>&nbsp; WriteString(Stream, ''); <br>end; <br><br>procedure WriteRowStart(Stream: TFileStream; IsAddedTitle: Boolean); <br>begin <br>&nbsp; if not IsAddedTitle then <br>&nbsp; &nbsp; WriteString(Stream, 'end; <br><br>procedure WriteRowEnd(Stream: TFileStream; IsAddedTitle: Boolean); <br>begin <br>&nbsp; if not IsAddedTitle then <br>&nbsp; &nbsp; WriteString(Stream, '/&gt;'); <br>end; <br><br>procedure WriteData(Stream: TFileStream; fld: TField; AString: ShortString); <br>begin <br>&nbsp; if Assigned(fld) and (AString &lt;&gt; '') then <br>&nbsp; &nbsp; WriteString(Stream, ' ' + fld.FieldName + '="' + AString + '"'); <br>end; <br><br>function GetFieldStr(Field: TField): string; <br><br>&nbsp; function GetDig(i, j: Word): string; <br>&nbsp; begin <br>&nbsp; &nbsp; Result := IntToStr(i); <br>&nbsp; &nbsp; while (Length(Result) &lt; j) do <br>&nbsp; &nbsp; &nbsp; Result := '0' + Result; <br>&nbsp; end; <br><br>var Hour, Min, Sec, MSec: Word; <br>begin <br>&nbsp; case Field.DataType of <br>&nbsp; &nbsp; ftBoolean: Result := UpperCase(Field.AsString); <br>&nbsp; &nbsp; ftDate: Result := FormatDateTime('yyyymmdd', Field.AsDateTime); <br>&nbsp; &nbsp; ftTime: Result := FormatDateTime('hhnnss', Field.AsDateTime); <br>&nbsp; &nbsp; ftDateTime: begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := FormatDateTime('yyyymmdd', Field.AsDateTime); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DecodeTime(Field.AsDateTime, Hour, Min, Sec, MSec); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Hour &lt;&gt; 0) or (Min &lt;&gt; 0) or (Sec &lt;&gt; 0) or (MSec &lt;&gt; 0) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := Result + 'T' + GetDig(Hour, 2) + ':' + GetDig(Min, 2) + ':' + GetDig(Sec, 2) + GetDig(MSec, 3); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; else <br>&nbsp; &nbsp; Result := Field.AsString; <br>&nbsp; end; <br>end; <br><br>&nbsp;<br><br>procedure DatasetToXML(Dataset: TDataset; FileName: string); <br>var <br>&nbsp; Stream: TFileStream; <br>&nbsp; bkmark: TBookmark; <br>&nbsp; i: Integer; <br>begin <br>&nbsp; Stream := TFileStream.Create(FileName, fmCreate); <br>&nbsp; SourceBuffer := StrAlloc(1024); <br>&nbsp; WriteFileBegin(Stream, Dataset); <br><br>&nbsp; with DataSet do <br>&nbsp; begin <br>&nbsp; &nbsp; DisableControls; <br>&nbsp; &nbsp; bkmark := GetBookmark; <br>&nbsp; &nbsp; First; <br><br>&nbsp; &nbsp; {write a title row} <br>&nbsp; &nbsp; WriteRowStart(Stream, True); <br>&nbsp; &nbsp; for i := 0 to FieldCount-1 do <br>&nbsp; &nbsp; &nbsp; WriteData(Stream, nil, Fields.DisplayLabel); <br>&nbsp; &nbsp; {write the end of row} <br>&nbsp; &nbsp; WriteRowEnd(Stream, True); <br><br>&nbsp; &nbsp; while (not EOF) do <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; WriteRowStart(Stream, False); <br>&nbsp; &nbsp; &nbsp; for i := 0 to FieldCount-1 do <br>&nbsp; &nbsp; &nbsp; &nbsp; WriteData(Stream, Fields, GetFieldStr(Fields)); <br>&nbsp; &nbsp; &nbsp; {write the end of row} <br>&nbsp; &nbsp; &nbsp; WriteRowEnd(Stream, False); <br><br>&nbsp; &nbsp; &nbsp; Next; <br>&nbsp; &nbsp; end; <br><br>&nbsp; &nbsp; GotoBookmark(bkmark); <br>&nbsp; &nbsp; EnableControls; <br>&nbsp; end; <br><br>&nbsp; WriteFileEnd(Stream); <br>&nbsp; Stream.Free; <br>&nbsp; StrDispose(SourceBuffer); <br>end; <br><br>end. <br><br><br>&nbsp;<br><br><br>&nbsp; 生成XML文件。<br>&nbsp; 我使用下面的转换方法:<br>&nbsp; I . &nbsp; XML文件的根名与表名相同(本例就是country)。<br>&nbsp; II. &nbsp; 每条来自于表的记录由&lt;record&gt;&lt;/record&gt;标记区分。<br>&nbsp; III. &nbsp;每个来自于表的数据由其字段名标记加以区分。<br>&nbsp; <br>&nbsp; - &lt;country&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - &lt;Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Name&gt;Argentina&lt;/Name&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Capital&gt;Buenos Aires&lt;/Capital&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Continent&gt;South America&lt;/Continent&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Area&gt;2777815&lt;/Area&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Population&gt;32300003&lt;/Population&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . <br>&nbsp; &nbsp; &nbsp;&lt;/country&gt; <br>&nbsp; <br>&nbsp; 建立一个新的应用程序。放置一个Button和Table构件于主窗体上。设置表属性如下:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DatabaseName : DBDEMOS <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name : Table1 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableName : country (Remove the extention ".db") <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Active : True <br>&nbsp; <br>&nbsp; 选择 Project/Import Type library。将会弹出 "Import Type Library" 对话框。从列表中选择 "Microsoft XML,Version <br>&nbsp; 2.0(version 2.0)" 然后点击 "Create Unit" 按钮。将会有一个 MSXML_TLB 单元加入你的工程.请将 MSXML_TLB 加入你要引用的单元的接口部分。然后在变量部分声明如下变量:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataList : TStringlist; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc : IXMLDOMDocument; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root,child,child1 : IXMLDomElement; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text1,text2 : IXMLDOMText; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nlist : IXMLDOMNodelist; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRecord : String; <br>&nbsp; <br>&nbsp; 添加makeXml函数到你的单元。它将通过读取DBDEMOS中contry表中的数据生成一个XML文件。<br>&nbsp; function TForm1.makeXml(table:TTable):Integer; <br>&nbsp; var <br>&nbsp; &nbsp; i &nbsp; : Integer; <br>&nbsp; &nbsp; xml,temp : String; <br>&nbsp; begin <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; table.close; <br>&nbsp; &nbsp; &nbsp; table.open; <br>&nbsp; &nbsp; &nbsp; xml &nbsp;:= table.TableName; <br>&nbsp; &nbsp; &nbsp; doc &nbsp;:= CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument; <br>&nbsp; &nbsp; &nbsp; //Set the root name of the xml file as that of the table name. <br>&nbsp; &nbsp; &nbsp; //In this case "country" <br>&nbsp; &nbsp; &nbsp; root := doc.createElement(xml); <br>&nbsp; &nbsp; &nbsp; doc.appendchild(root); <br>&nbsp; &nbsp; &nbsp; //This while loop will go through the entaire table to generate the xml file <br>&nbsp; &nbsp; &nbsp; while not table.eof do <br>&nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; //adds the first level children , Records <br>&nbsp; &nbsp; &nbsp; &nbsp; child:= doc.createElement('Records'); <br>&nbsp; &nbsp; &nbsp; &nbsp; root.appendchild(child); <br>&nbsp; &nbsp; &nbsp; &nbsp; for i:=0 to table.FieldCount-1 do <br>&nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //adds second level children <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child1:=doc.createElement(table.Fields.FieldName); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child.appendchild(child1); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Check field types <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TFieldType(Ord(table.Fields.DataType)) of <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftString: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Table.Fields.AsString ='' then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp :='null' &nbsp;//Put a default string <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp := table.Fields.AsString; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftInteger, ftWord, ftSmallint: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Table.Fields.AsInteger &gt; 0 then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp := IntToStr(table.Fields.AsInteger) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp := '0'; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftFloat, ftCurrency, ftBCD: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if table.Fields.AsFloat &gt; 0 then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp := FloatToStr(table.Fields.AsFloat) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp := '0'; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftBoolean: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if table.Fields.Value then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:= 'True' <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:= 'False'; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftDate: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (not table.Fields.IsNull) or <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(Length(Trim(table.Fields.AsString)) &gt; 0) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp := FormatDateTime('MM/DD/YYYY', <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table.Fields.AsDateTime) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp:= '01/01/2000'; //put a valid default date <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftDateTime: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (not table.Fields.IsNull) or <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(Length(Trim(table.Fields.AsString)) &gt; 0) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp := FormatDateTime('MM/DD/YYYY hh:nn:ss', <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Table.Fields.AsDateTime) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp := '01/01/2000 00:00:00'; //Put a valid default date and time <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftTime: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (not table.Fields.IsNull) or <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(Length(Trim(table.Fields.AsString)) &gt; 0) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp := FormatDateTime('hh:nn:ss', <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table.Fields.AsDateTime) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp := '00:00:00'; //Put a valid default time <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;child1.appendChild(doc.createTextNode(temp)); <br>&nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; table.Next; <br>&nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; doc.save(xml+'.xml'); <br>&nbsp; &nbsp; &nbsp; memo1.lines.Append(doc.xml); <br>&nbsp; &nbsp; &nbsp; Result:=1; <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; &nbsp; on e:Exception do <br>&nbsp; &nbsp; &nbsp; &nbsp; Result:=-1; <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; 在Button1的onclick事件中调用上面的函数<br>&nbsp; procedure TForm1.Button1Click(Sender: TObject); <br>&nbsp; begin <br>&nbsp; &nbsp; if makeXml(table1)=1 then <br>&nbsp; &nbsp; &nbsp; showmessage('XML Generated') <br>&nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; showmessage('Error while generating XML File'); <br>&nbsp; end; <br>&nbsp; <br>&nbsp; 如果你用IE 5.0(或以上版本)打开生成的country.xml文件,它看起来会成下面的样子<br>&nbsp; - &lt;country&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - &lt;Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Name&gt;Argentina&lt;/Name&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Capital&gt;Buenos Aires&lt;/Capital&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Continent&gt;South America&lt;/Continent&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Area&gt;2777815&lt;/Area&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Population&gt;32300003&lt;/Population&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - &lt;Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Name&gt;Bolivia&lt;/Name&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Capital&gt;La Paz&lt;/Capital&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Continent&gt;South America&lt;/Continent&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Area&gt;1098575&lt;/Area&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Population&gt;7300000&lt;/Population&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - &lt;Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Name&gt;Venezuela&lt;/Name&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Capital&gt;Caracas&lt;/Capital&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Continent&gt;South America&lt;/Continent&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Area&gt;912047&lt;/Area&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Population&gt;19700000&lt;/Population&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/Records&gt; <br>&nbsp; &nbsp; &lt;/country&gt; <br>&nbsp; <br>&nbsp; 插入数据<br>&nbsp; <br>&nbsp; 你已经将country表中存在的数据生成了XML文件。因此在这个XML文件中的数据就与country表中是一样的。如果你想将XML文件中的数据插入进country表中又不想删除原来存在的数据的话,将会有主键冲突的错误出现。因此必须先将country表中已经存在的数据删除掉。<br>&nbsp; 添加另一个按钮和一个memo构件于主窗体。在button2的onclick事件中添加如下代码.memo用来显示数据插入中的状态(成功/失败)。<br>&nbsp; procedure TForm1.Button2Click(Sender: TObject); <br>&nbsp; var <br>&nbsp; &nbsp; &nbsp;i,ret_val,count:Integer; <br>&nbsp; &nbsp; &nbsp;strData:String; <br>&nbsp; begin <br>&nbsp; &nbsp; &nbsp; //Before inserting data in to the country table,make sure that the data in <br>&nbsp; &nbsp; &nbsp; //the generated xml file(country.xml) and country table(DBDEMOS) are <br>&nbsp; &nbsp; &nbsp; //different. <br>&nbsp; &nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; &nbsp; count:=1; <br>&nbsp; &nbsp; &nbsp; &nbsp; DataList:=TStringList.Create; <br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.Clear; <br>&nbsp; &nbsp; &nbsp; &nbsp; doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Load country.xml file <br>&nbsp; &nbsp; &nbsp; &nbsp; doc.load('country.xml'); <br>&nbsp; &nbsp; &nbsp; &nbsp; nlist:=doc.getElementsByTagName('Records'); <br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.append('Table Name &nbsp;:country'); <br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.append('---------------------'); <br>&nbsp; &nbsp; &nbsp; &nbsp; for i:=0 to nlist.Get_length-1 do <br>&nbsp; &nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;travelChildren(nlist.Get_item(i).Get_childNodes); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Removes the first character(,) from dataRecord <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strData:=copy(dataRecord,2,length(dataRecord)); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.append(strData); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataRecord:=''; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret_val:=insertintotable(Datalist); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ret_val=1 then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.append('Data inserted successfully.............!') <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ret_val=-1 then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.append('Error while updating.....Try again.....!'); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.append('=============================================' <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'==(Record no. :'+inttostr(count)+')'); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataList.Clear; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count:=count+1; <br>&nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; except <br>&nbsp; &nbsp; &nbsp; &nbsp; on e:Exception do <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Showmessage(e.message); <br>&nbsp; &nbsp; &nbsp;end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; nlist(refer above program) contains a list of nodes.In our case the first node list is... <br>&nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Records&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Name&gt;Argentina&lt;/Name&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Capital&gt;Buenos Aires&lt;/Capital&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Continent&gt;South America&lt;/Continent&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Area&gt;2777815&lt;/Area&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;Population&gt;32300003&lt;/Population&gt; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/Records&gt; <br>&nbsp; <br>&nbsp; <br>&nbsp; 我们传送此节点列表给一个递归函数,travelchildren。它将递归地沿着节点列表查找文本数据,并将此数据加入TStringList(Datalist)变量中。当完成第一轮后,Datalist中将会包含字符串 Argentina,Buenos Aires,South America,2777815,32300003.最后我们将此stringlist传送给函数 insertintotable,它将完成将一条记录插入 country 表的工作。重复此过程即可完成整个XML文件数据的插入工作。<br>&nbsp; procedure TForm1.travelChildren(nlist1:IXMLDOMNodeList); <br>&nbsp; var <br>&nbsp; &nbsp; &nbsp;j:Integer; <br>&nbsp; &nbsp; &nbsp;temp:String; <br>&nbsp; begin <br>&nbsp; &nbsp; for j:=0 to nlist1.Get_length-1 do <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; //node type 1 means an entity and node type 5 means EntityRef <br>&nbsp; &nbsp; if((nlist1.Get_item(j).Get_nodeType= 1) or (nlist1.Get_item(j).Get_nodeType=5)) then <br>&nbsp; &nbsp; &nbsp; travelChildren(nlist1.Get_item(j).Get_childNodes) <br>&nbsp; &nbsp; &nbsp; //node Type 3 means a text node,ie you find the data <br>&nbsp; &nbsp; &nbsp; else if(nlist1.Get_item(j).Get_nodeType=3) then <br>&nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; temp:= trim(nlist1.Get_item(j).Get_nodeValue); <br>&nbsp; &nbsp; &nbsp; &nbsp; dataRecord:=dataRecord+','+temp; //this is for displaying a single record on the memo <br>&nbsp; &nbsp; &nbsp; &nbsp; DataList.Add(temp); //Datalist will contain one record after completing one full travel through the node list <br>&nbsp; &nbsp; &nbsp; end <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; function TForm1.insertintotable(stpt:TStringList):Integer; <br>&nbsp; var <br>&nbsp; &nbsp; i:Integer; <br>&nbsp; begin <br>&nbsp; &nbsp; table1.close; <br>&nbsp; &nbsp; table1.open; <br>&nbsp; &nbsp; table1.Insert; <br>&nbsp; &nbsp; for i := 0 to stpt.Count - 1 do <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table1.Fields.AsVariant:= stpt; <br>&nbsp; &nbsp; end; <br>&nbsp; &nbsp; try <br>&nbsp; &nbsp; &nbsp; table1.post; <br>&nbsp; &nbsp; &nbsp; result:=1; <br>&nbsp; &nbsp; except <br>&nbsp; &nbsp; &nbsp; on E:Exception do <br>&nbsp; &nbsp; &nbsp; &nbsp; result:=-1; <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; 结论:<br>&nbsp; 你可以将此程序推广至任何数据库,由此数据可以通过XML文件在网络(即使是internet)中传输并在其实终端上更新数据库。我在生成XML文件中还未考虑特殊字符如 &amp;,&lt;,&gt;,',''等等。你可以在生成带这些字符的XML文件时作适合自己需要的改变.
 
to szhcracker:你并没有回答我的问题啊,SQL SERVER转为XML不是问题,问题是XML中的IMAGE字段内容如何转回SQL SERVER库。<br>我是用C#做的,SQL SERVER转为XML远没有你写的这么复杂,几行代码足够了
 
delphi里面 dataset 可以 savetoxml ,也可以load <br>C# 里面的话 ,既然你能几句 outport ,难道就不能import ?
 
我想bsense误解了我的意思,我是把一张表的内容存到XML文件中,表中有一个字段是IMAGE类型的,现在的问题是怎么把这一字段的内容导回数据库。 <br>而不是说想把整个XML文件存到一张表的IMAGE字段中,那也太容易了。
 
路过...顶一个..
 
skinfeature界面产品特点介绍<br><br>SkinFeature完全支持各种常用控件及窗口 。对话框,单文档界面,多文档界面的全部支持。 <br>完全支持PNG、TGA、bmp 图像格式。 <br>支持皮肤(.rss)文件加密。保护美工图像不被别人非法使用,进一步保护自身知识产权。 <br>支持从msstyle wba 等主题文件中自动转化为rss皮肤文件。 <br>SkinFeature完全支持 VC,Delphi,C#,VB.Net,Visual Basic,C++ Bulider, PowerBuilder, Win32 SDK Supported WTL, ATL, 第三方厂商SDK, OUTLOOK 等等界面换肤。 <br>支持各Windows平台 Windows 9X/NT/2000/2003/XP/Vista <br>完全支持ANSI UNICODE 文字编码格式。 <br>完全支持动态换肤(*.rss),支持不规则窗体,支持美工设计自定义界面生成方案。 <br>完全多线程,自定义窗口类换肤支持。 <br>内含15种专业皮肤(.rss)文件。 <br>内含SkinDesigner 皮肤文件开发工具。<br>&nbsp;<br>http://www.skinfeature.com
 
应该是将FORM的IMAGE以二进制的格式保存在XML中,然后以其为中介导入到SQL SERVER中吧
 
从其他地方找到了解决方法,散分了
 
多人接受答案了。
 
顶部