13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The SqlXmlAdapter ClassThe SqlXmlAdapter class is a shr<strong>in</strong>k-wrapped adapter class. It does not implement theIDataAdapter <strong>in</strong>terface, so technically speak<strong>in</strong>g, it can't be presented as an adapterobject. Nevertheless, the class provides adapter-like methods such as Fill and Update,as shown <strong>in</strong> the follow<strong>in</strong>g code. These are also the only public methods <strong>for</strong> the class.void Fill(DataSet ds);void Update(DataSet ds);The SqlXmlAdapter class also provides three constructors, shown <strong>in</strong> the follow<strong>in</strong>g code,whose signatures re<strong>in</strong><strong>for</strong>ce the idea that this adapter is a mere wrapper class <strong>for</strong>SqlXmlCommand. In other words, the SqlXmlAdapter class is more a command thatmanages DataSet objects than a true data adapter object as it is described <strong>in</strong> theADO.<strong>NET</strong> specification.public SqlXmlAdapter(SqlXmlCommand cmd)public SqlXmlAdapter(str<strong>in</strong>g commandText,SqlXmlCommandType cmdType,str<strong>in</strong>g connectionStr<strong>in</strong>g)public SqlXmlAdapter(Stream commandStream,SqlXmlCommandType cmdType,str<strong>in</strong>g connectionStr<strong>in</strong>g)These constructors use the <strong>in</strong><strong>for</strong>mation they receive to set up an <strong>in</strong>ternal <strong>in</strong>stance of theSqlXmlCommand class. The Fill method makes full use of all the <strong>in</strong><strong>for</strong>mation passedthrough the constructor. For the Update method, on the other hand, only the connectionstr<strong>in</strong>g <strong>in</strong><strong>for</strong>mation is actually needed.Fill<strong>in</strong>g an <strong>XML</strong> AdapterThe Fill method is rather simple. First it executes the embedded <strong>XML</strong> command us<strong>in</strong>gExecuteStream. Next it uses the returned memory stream to fill the specified DataSetobject through its ReadXml method.The ReadXml method populates a DataSet object by read<strong>in</strong>g <strong>XML</strong> data from a varietyof sources, <strong>in</strong>clud<strong>in</strong>g streams and text readers, and <strong>in</strong>ferr<strong>in</strong>g the schema. We'll exam<strong>in</strong>ethe <strong>in</strong>ference process <strong>in</strong> detail <strong>in</strong> Chapter 9. For now, suffice to say that ReadXml candetect any <strong>in</strong>-l<strong>in</strong>e or referenced XSD schema or determ<strong>in</strong>e the schema dynamically.Once the DataSet object has been filled from the <strong>XML</strong> stream generated by thecommand execution, all the changes are accepted so that the DataSet object appears<strong>in</strong>tact and with no pend<strong>in</strong>g changes.Updat<strong>in</strong>g Us<strong>in</strong>g an <strong>XML</strong> AdapterThe Update method takes a DataSet object and applies its pend<strong>in</strong>g changes to thetarget database. The parameters specified on <strong>in</strong>stantiation conta<strong>in</strong> the details about theconnection str<strong>in</strong>g. The embedded SqlXmlCommand object has command text and acommand type that are simply ignored dur<strong>in</strong>g Update. Let's see why.When Update executes, the embedded command object is used to per<strong>for</strong>m the task,but its command text and command type properties are silently and temporarilyoverwritten with DataSet-specific sett<strong>in</strong>gs.The Update method writes the contents of the DataSet object to a newly createdmemory stream. The DataSet object is serialized as a DiffGram. Next the contents ofthe stream—that is, the DiffGram representation of the DataSet object—are copied <strong>in</strong>tothe CommandText property of the underly<strong>in</strong>g SqlXmlCommand object. TheCommandType property is set to Template, and ExecuteStream is called to update the321

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!