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.

schema <strong>in</strong><strong>for</strong>mation to associate the element with a record <strong>in</strong> a table. The record isactually written when the end tag <strong>for</strong> that element is found. This algorithm ensures that<strong>in</strong> the case of parent-child relationships, all the children are processed be<strong>for</strong>e theparent row.Transacted Load<strong>in</strong>gUnlike the T-SQL BULK INSERT statement, <strong>XML</strong> bulk load<strong>in</strong>g is a sort of add-on.Because <strong>XML</strong> bulk load<strong>in</strong>g is not natively part of SQL Server 2000, it never runs with<strong>in</strong>an implicit transaction, as normally happens with T-SQL statements. As a result, youmust manage transactions yourself. On the other hand, bulk load<strong>in</strong>g is the k<strong>in</strong>d ofoperation that sometimes does need to run <strong>in</strong> a transacted context.It goes without say<strong>in</strong>g that if you can af<strong>for</strong>d to run bulk load<strong>in</strong>g without transactions,do<strong>in</strong>g so would be greatly beneficial to the overall per<strong>for</strong>mance of the application.Nontransacted load<strong>in</strong>g makes a lot of sense when you have to fill up empty databases.In a transactionless scenario, you lose the ability to roll back changes, but becauseyour databases were orig<strong>in</strong>ally empty, if someth<strong>in</strong>g goes wrong, you can clear thedatabase and start over.NoteIn nontransacted mode, <strong>XML</strong> bulk load<strong>in</strong>g takes advantage of themethods of the OLE DB IRowsetFastLoad <strong>in</strong>terface to do the job.Not all OLE DB providers supply the IRowsetFastLoad <strong>in</strong>terface, butthe SQLOLEDB provider does.When <strong>XML</strong> bulk load<strong>in</strong>g works <strong>in</strong> transacted mode, the component creates a temporaryfile <strong>for</strong> each table <strong>in</strong>volved <strong>in</strong> the operation. The files will gather all the changes <strong>for</strong> thetables. When a commit occurs, the contents of the various files are flushed <strong>in</strong>to thecorrespond<strong>in</strong>g SQL Server table us<strong>in</strong>g the BULK INSERT statement.<strong>XML</strong> Bulk Load<strong>in</strong>g <strong>in</strong> ActionLet's see how <strong>XML</strong> bulk load<strong>in</strong>g really works. As mentioned, <strong>XML</strong> bulk load<strong>in</strong>g isimplemented through a COM object whose progID attribute is SQL<strong>XML</strong>BulkLoad. Thefollow<strong>in</strong>g Visual Basic 6.0 code shows how to use the object:conn = "PROVIDER=sqloledb;SERVER=localhost;" & _"database=Northw<strong>in</strong>d;UID=sa"Set bulk = CreateObject("SQL<strong>XML</strong>BulkLoad.SQL<strong>XML</strong>Bulkload.3.0")bulk.ConnectionStr<strong>in</strong>g = connbulk.Execute "schema.xml", "data.xml"To per<strong>for</strong>m bulk load<strong>in</strong>g, you set the connection str<strong>in</strong>g and then call the Executemethod. The method takes two arguments: the schema and the <strong>XML</strong> source data. Inl<strong>in</strong>eschemas are ignored, as are schema files referenced <strong>in</strong> the source file. As a result,you must always supply schema <strong>in</strong><strong>for</strong>mation and data through dist<strong>in</strong>ct <strong>XML</strong> files.F<strong>in</strong>ally, note that <strong>XML</strong> documents are checked <strong>for</strong> well-<strong>for</strong>medness, but their contentsare never validated aga<strong>in</strong>st any schema. Any contents outside the root node of thedocument—the node—are simply discarded.The follow<strong>in</strong>g list<strong>in</strong>g shows a typical source <strong>for</strong> a bulk load<strong>in</strong>g operation. It adds acouple of employees, each with a few related territories.308

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

Saved successfully!

Ooh no, something went wrong!