Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADO(Active Data Objects), ADO+, and XML

Similar presentations


Presentation on theme: "ADO(Active Data Objects), ADO+, and XML"— Presentation transcript:

1 ADO(Active Data Objects), ADO+, and XML
석사 2학기 최혜정

2 Contents Introduce XML Support in ADO 2. 6 and SQL Server 2000
XML Persistence XML Queries via ADO Non-SQL Queries via ADO Joining XML and SQL Data ADO+ Summary

3 Introduce How ADO has progressed to include more robust XML feature set than in the past. Here we will discuss that’s supported in ADO 2.6, and its new XML properties. Different ways in which we can persist XML via ADO, and how the Stream makes some of this possible, including a look at the ASP Response object. How we can query SQL Server and return XML data, both using extensions to the SQL language, and querying using Xpath and mapping schemas. How we can merge XML data sets with SQL data sets to retrieve and even modify SQL data. Take a sneak preview look at what’s coming in the future, by investigate ADO+

4 XML Support in ADO 2. 6 and SQL Server 2000 (1/2)
The Stream Object (a stream of text or binary bytes) to read , write, and manage the binary stream of bytes that make up a file or message. Can take a RecordSet and save it the XML format to a Stream Object. Can send XML directly to the Response Object, thus writing it to a client’s browser. Can load different types of queries into a Stream Object and execute them against a SQL Server database. Obtaining a Stream Dim oStm Set oStm = Server.CreateObject(“ADODB.Stream”) 1. oStm.Open = “URL= 2. oStml.Open=“d:\customer.xml” 3. sXML =“<costmer CustID=‘123’ CustName=‘XYZ’ Records’></customer>” oStm.Open oStm.WriteText sXML

5 XML Support in ADO 2. 6 and SQL Server 2000 (2/2)
Persistence of XML A file (XML file), SQL Server 2000, Send across a network, ASP’s Response object Running XML Queries Queries Using For XML ; SQL Server 2000’s Transact SQL(T-SQL) Queries Using annotated schema ; Xpath statement Merging XML with relational data T-SQL language:OpenXML function allows stored procedure. New XML Properties Mapping Schema ; oCmd.Properties(“Mapping Schema”) = “Orders.xml” Base Path ; oCmd.Properties(“Base Path”) = “C:\InetPub\wwwroot\XMLDM\” Output Stream ; oCmd.properties(“Output Stream”) = Response

6 Table Schema

7 XML Persistence (1/6) Persisting to a file Persisting to a Stream
Saving a file from RecordSet Opening a Recordset from a file Persisting to a Stream Opening a Recordset from a Stream Persisting to the Response Object Writing a Stream to a Client Persisting a Stream to a Client

8 Saving a file from RecordSet(2/6)
<!-- #include file="adovbs.inc" --> <% sConn = "Data Source=(local);Initial Catalog=Northwind;" & _ "User ID=sa;Password=;" sProvider = "SQLOLEDB" Set oCn = Server.CreateObject("ADODB.Connection") oCn.Provider = sProvider oCn.ConnectionString = sConn oCn.CursorLocation = adUseClient oCn.Open Set oCmd = CreateObject("ADODB.Command") Set oCmd.ActiveConnection = oCn oCmd.CommandText = "SELECT CustomerID, CompanyName FROM Customers" oCmd.CommandType = adCmdText Set oRs = oCmd.Execute() oRs.Save "c:\Customers.xml", adPersistXML oRs.Close %>

9 Opening a Recordset from a file(3/6)
Set oRs = Server.CreateObject("ADODB.Recordset") Set oRs.ActiveConnection = Nothing oRs.LockType = adLockReadOnly oRs.Source = "c:\Customers.xml" oRs.Open sTable = "<table border='1'>" & "<tr>" For i = 0 to oRs.Fields.Count - 1 sTable = sTable & "<td><b>" & oRs.Fields(i).Name & "</b></td>" Next ‘oRs.MoveNext sTable = sTable & "</tr>" Do While Not oRs.EOF sTable = sTable & "<tr>" sTable = sTable & "<td>" & oRs.Fields(i).Value & "</td>" oRs.MoveNext sTable = sTable & "</tr>" Loop sTable = sTable & "</table>" Response.Write sTable oRs.Close Set oRs = Nothing

10 Opening a Recordset from a Stream(4/6)
Dim oStm Dim sXML Set oStm = Server.CreateObject("ADODB.Stream") oStm.Charset = "Windows-1252" oStm.Open oStm.LoadFromFile "c:\customers.xml" sXML = oStm.ReadText(adReadAll) oStm.Close 1. Set oStm = Server.CreateObject("ADODB.Stream") oStm.WriteText sXML oStm.SaveToFile "c:\test.xml", adSaveCreateOverWrite 2. sTable = …. Response.Write sTable Set oStm = Nothing

11 Writing a Stream to a Client(5/6)
<% Set oStm = Server.CreateObject("ADODB.Stream") oStm.Charset = "Windows-1252" oStm.Open oStm.LoadFromFile "c:\Territories.xml" sXML = oStm.ReadText(adReadAll) oStm.Close Set oStm = Nothing Response.Write "<XML ID='MyDataIsle'>" Response.Write sXML Response.Write "</XML>“ %> <SCRIPT language="VBScript" For="window" Event="onload"> Set oXML = MyDataIsle.XMLDocument oXML.resolveExternals=false oXML.async=false Set oRoot = oXML.documentElement For each oChild in oRoot.childNodes sOutputXML = document.all("log").innerHTML document.all("log").innerHTML = sOutputXML & "<LI>" & _ oChild.getAttribute("TerritoryDescription") & "</LI>" Next </SCRIPT> <body> <UL id=log> </UL> </body>

12 Persisting a Stream to a Client(6/6)
sConn = "Data Source=(local);Initial Catalog=Northwind;" & _ "User ID=sa;Password=;" sProvider = "SQLOLEDB" Set oCn = Server.CreateObject("ADODB.Connection") oCn.Provider = sProvider oCn.ConnectionString = sConn oCn.CursorLocation = adUseClient oCn.Open Set oCmd = CreateObject("ADODB.Command") Set oCmd.ActiveConnection = oCn oCmd.CommandText = "SELECT * FROM Territories" oCmd.CommandType = adCmdText Set oRs = oCmd.Execute() oRs.Save Response, adPersistXML oRs.Close oCn.Close Set oRs = Nothing ..

13 XML Queries via ADO(1/5) For XML Usage For XML via ADO
Have the ability to return XML data sets from SQL queries simply by appending the FOR XML clause Don’t have to create the extra objects such as the RecordSet object or the XML DOM object. For XML Template Queries For XML URL Queries For XML via ADO Running a For XML Command Running FOR XML via CommandText Running FOR XML via CommandStream

14 Virtual Directory Setting(2/5)
See Appendix E MS SQL Serverl -> SQL Server용 IIS 가상 디렉토리 관리

15 For XML Usage(3/5) 1. For XML Template Queries
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <sql:query> SELECT Customers.CustomerID, Customers.CompanyName, Orders.OrderID, Orders.OrderDate FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID ORDER BY Customers.CompanyName FOR XML AUTO </sql:query> </ROOT>

16 Running FOR XML via CommandText(4/5)
<% sProvider = "SQLOLEDB" sConn = "Data Source=(local);Initial Catalog=Northwind;User ID=sa" Set oCn = Server.CreateObject("ADODB.Connection") …. oCn.Open Set oCmd = Server.CreateObject("ADODB.Command") Set oCmd.ActiveConnection = oCn sQuery = "<ROOT xmlns:sql='urn:schemas-microsoft-com:xml-sql'>" & _ "<sql:query>SELECT * FROM PRODUCTS ORDER BY PRODUCTNAME " & _ " FOR XML AUTO </sql:query></ROOT>" oCmd.CommandText = sQuery oCmd.CommandType = adCmdText oCmd.Dialect = "{5D531CB2-E6Ed-11D2-B252-00C04F681B71}" oCmd.Properties("Output Stream") = Response Response.Write "<XML ID=MyDataIsle>" oCmd.Execute , , adExecuteStream Response.Write "</XML>" %> <SCRIPT language="VBScript" For="window" Event="onload"> … oChild.getAttribute(“ProductName”)…. </SCRIPT> <body> <UL id=log> </UL> </body>

17 Running FOR XML via CommandStream(5/5)
… sConn = "Data Source=(local);Initial Catalog=Northwind;User ID=sa" Set oCn = Server.CreateObject("ADODB.Connection") …. oCn.Open Set oCmd = Server.CreateObject("ADODB.Command") Set oCmd.ActiveConnection = oCn sQuery = "<ROOT xmlns:sql='urn:schemas-microsoft-com:xml-sql'>" & _ "<sql:query>SELECT * FROM PRODUCTS ORDER BY PRODUCTNAME " & _ " FOR XML AUTO </sql:query></ROOT>" Set oStm = Server.CreateObject("ADODB.Stream") oStm.Open oStm.WriteText sQuery, adWriteChar oStm.Position = 0 set oCmd.CommandStream = oStm oCmd.CommandType = adCmdText oCmd.Dialect = "{5D531CB2-E6Ed-11D2-B252-00C04F681B71}" oCmd.Properties("Output Stream") = Response Response.Write "<XML ID=MyDataIsle>" oCmd.Execute , , adExecuteStream Response.Write "</XML>" %> <SCRIPT language="VBScript" For="window" Event="onload"> … oChild.getAttribute(“ProductName”)…. </SCRIPT> <body> <UL id=log> </UL> </body>

18 Non-SQL Queries via ADO(1/2)
Mapping Schema File Define the table schema Mapping Schema & Base Path Properties Code Examples

19 Code Examples(2/2) sCompanyName = Request.QueryString("CompanyName") …
Set oCn = Server.CreateObject("ADODB.Connection") oCn.Open Set oCmd = CreateObject("ADODB.Command") Set oCmd.ActiveConnection = oCn oCmd.CommandText = = '" & sCompanyName & "']" oCmd.Dialect = "{ec2a4293-e898-11d2-b1b7-00c04f680c56}" '--- Indentify the mapping schema oCmd.Properties("Mapping Schema") = "ch13_ex10.xml" oCmd.Properties("Base Path") = "C:\" oCmd.Properties("Output Stream") = Response Response.write "<XML ID='MyDataIsle'><Customers>" oCmd.Execute , , adExecuteStream Response.write "</Customers></XML>" <SCRIPT language="VBScript"> document.write … oCurrentCustomer.getAttribute("CustID") …. oChild.getAttribute("OrderDate") </SCRIPT>

20 Joining XML and SQL Data(1/5)
Stored Procedures Sp_xml_preparedocument Translate the XML document to a data set format that SQL Server can comprehend Sp_xml_removedocument Removes the XML document from SQL Server’s memory when we’re done with it The ASP page Returning XML Inserting Data

21 Stored Procedures(2/5)

22 The ASP page(3/5) create proc dbo.spOpenXML @sXML VarChar(2000) as
INT EXEC Select c.CustomerID, c.ContactName, o.OrderID, o.OrderDate from '/ROOT/Customers', 1) with (CustomerID varchar(10), ContactName varchar(20)) as c INNER JOIN Orders o ON c.CustomerID = o.CustomerID EXEC GO <% sXMLDoc = "<ROOT>" sXMLDoc = sXMLDoc & "<Customers CustomerID='VINET' " & _ "ContactName='Paul Henriot'>“ …. sXMLDoc = sXMLDoc & "</ROOT>" sQuery = "spOpenXML" Set oCmd = Server.CreateObject("ADODB.Command") Set oCmd.ActiveConnection = oCn oCmd.CommandText = sQuery oCmd.CommandType = adCmdStoredProc = sXMLDoc Set oRs = oCmd.Execute() Response.Write "<ul>“ …. %>

23 Returning XML(4/5) create proc dbo.spOpenXML2 @sXML VarChar(2000) as
INT EXEC Select c.CustomerID, c.ContactName, o.OrderID, o.OrderDate from '/ROOT/Customers', 1) with (CustomerID varchar(10), ContactName varchar(20)) as c INNER JOIN Orders o ON c.CustomerID = o.CustomerID FOR XML AUTO EXEC GO sQuery = "spOpenXML2" oCmd.CommandText = sQuery oCmd.CommandType = adCmdStoredProc = sXMLDoc Response.Write "<XML ID='MyDataIsle'><ROOT>" oCmd.Execute , , adExecuteStream Response.Write "</ROOT></XML>“ %> <SCRIPT language="VBScript" For="window" Event="onload"> ….oChild.getAttribute("ContactName") & "</LI>" </SCRIPT> <body> <UL id=log> </UL> </body>

24 Inserting Data(5/5) create proc dbo.spOpenXML_Insert
@sXML Varchar(2000) as INT EXEC Insert Territories Select TerritoryID, TerritoryDescription, RegionID from '/ROOT/Territories', 1) with ( TerritoryID nvarchar(20), TerritoryDescription nchar(50), RegionID INT) EXEC Go <% sXMLDoc = "<ROOT>" sXMLDoc = sXMLDoc & "<Territories TerritoryID='77777' " & _ …. </ROOT>" sQuery = "spOpenXML_Insert" … oCmd.Execute %>

25 ADO+(1/3) Evolution The ADO+ Framework (objects) The Idea DataSet
Provided an automation API for abstracted database access for client server system. (n-tier systems) The ADO+ Framework (objects) DataSet A DataSet can store many pieces of data. It is easy to remote. And it is easy to program. Connection ( ADOConnection) is familiar and parallel to the classic ADO Connection Object. is compatible with OLEDB DataSetCommand Pushes data into a DataSet from a Database. SelectCommand, UpdateCommand, InsertCommand, DeleteCommand

26 ADO+(2/3) DataSet ( collection of tables)
Bridge the gap between the XML Web world and traditional corporate databases. The Command object provides interfaces for streaming read-only forward only data. Schema XMLSchema XMLData XML The DataSetCommand and the Connection Objects VB Typeness of DataSet Not only does the DataSet have internal typed storage, but there are also some nice tools to create typed accessors customized to your schema.

27 Typeness of DataSet(3/3)


Download ppt "ADO(Active Data Objects), ADO+, and XML"

Similar presentations


Ads by Google