1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional XML Databases phần 7 potx

84 197 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 84
Dung lượng 536,71 KB

Nội dung

ADO, ADO+, and XML 527 When working with the .NET framework you can easily move DataSets around because they are inherently disconnected. We can write a function that returns our dataset: Public Function GetCustomersList() as DataSet Dim sqlConnection as New SQLConnection("server=(local);uid=sa;database=northwind ") Dim dsCommandSet as New SQLDataSetCommand("select * from customers",sqlConnection) Dim dsCustomers as new DataSet dsCommandSet.FillDataSet(dsCustomers,"Customers") GetCustomersList = dsCustomers End Function Applications can expose DataSets in this manner. However, a useful side-effect of DataSets is that when used as a Web Service, they expose XML. A Web Service in this context can be summed up as a function that can be called from the Web (from an HTTP request). By attributing the above function as a Web function, a URL is created to route the request from the function. It is not clear at the time of this writing how and what attributes will be required. Attributes are a new concept to VB and decorate a function or code with more meaning without polluting the language. When the function is invoked by another .NET component, a DataSet is returned. When the function is invoked by a non NET component, the data is returned as an XML document. If you simply want to force the return of XML in a string format, declare the function as String, and then return dsCustomers.XMLData. If changes are made to the DataSet that is filled by a DataSetCommand, that same DataSetCommand can reconcile those changes with the Database, using the Update method. dsCommandSet.FillDataSet(dsCustomers,"Customers") dsCustomers.tables("customers").Rows(0)("CompanyName") = "foo" '… 'Remote the dataset 'send it in mail 'change it in any way 'at some point send it back and then '… dsCommandSet.Update(dsCustomers,"Customers") If you have stored procedures or SQL statements associated with the insert/update and delete commands, these will fire appropriately when the update method is invoked. You also have the ability to just send the changes (using the GetChanges method on the DataSet), as the Update method simply ignores rows that haven't been changed. Chapter 13 528 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. These typed accessors are further help in obtaining compile time type checking bugs instead of run-time; which overcomes a great source of stress in the current variant-based data access models. The idea behind a typed accessor for a DataSet is, that you can program directly against the tables and columns through the name and type of data structure (that is, Customer(0).FirstName). So if, as before, we wanted to access the data in our "generic" Dataset, we would use notation such as the following: Just to get at the data, you can access the objects by tables and column names (there is a default 0 based index property, as well as PrimaryKey property). Response.Write(dsPurchaseOrders.Customers(0).FirstName) 'Or Response.Write(dsPurchaseOrders.Customers.PrimaryKey(10001010).FirstName It is also very clean when you iterate through the relationships. We can now use the following syntax: For each Customer in dsPurchaseOrders.Customers For Each Order in Customer.Orders Response.Write(Order.OrderDate) Next Next These typed accessors are available essentially through code wrappers only. However, there are some very nice tools, which allow easy and efficient creation of them. The first tool is that of the language. VB.NET introduces some new object oriented programming techniques such as, and most important for this discussion, inheritance. By inheriting from System.Data.DataSet, we can easily add accessors to the columns and tables allowing inherited calls to pass through to the parent Dataset objects. Visual Studio 7 introduces a new XML Schema designer used to create XML schema and data. The tool allows for many scenarios, including mapping databases to XML schema, creating XML schema from databases, etc. Once the format is in XML Schema, a code generator (xsd.exe) can be invoked to create the classes that inherit from DataSet and provide the typed accessors mentioned above. This code generator is part of the framework and can be used to form the command line, as illustrated here. The executable simply generates a wrapper class that inherits from DataSet, and has accessors consistent with the schema of the XML, including relationships (interpreted from hierarchies or schema). This class, when included with your project, will provide the programming model as sampled above. Using the DataSet created above against the customers table in the database, I grabbed the .XMLSchema property and created a file: C:\>customers.xsd <schema id="" targetNamespace="" xmlns="http://www.w3.org/1999/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <element name="Customers"> <complexType content="elementOnly"> <all> <element name="CustomerID" type="string"/> <element name="CompanyName" type="string"/> ADO, ADO+, and XML 529 <element name="ContactName" minOccurs="0" type="string"/> <element name="ContactTitle" minOccurs="0" type="string"/> <element name="Address" minOccurs="0" type="string"/> <element name="City" minOccurs="0" type="string"/> <element name="Region" minOccurs="0" type="string"/> <element name="PostalCode" minOccurs="0" type="string"/> <element name="Country" minOccurs="0" type="string"/> <element name="Phone" minOccurs="0" type="string"/> <element name="Fax" minOccurs="0" type="string"/> </all> </complexType> </element> </schema> Notice the database schema generated an XML (XSD versioned) schema. The class generator, XSD, can be found in the \bin directory of the FrameworksSDK. This will generate both general classes (using the /c switch) and subsetted Dataset classes (using the /d switch), as seen below. C:\>xsd.exe customers.xsd /d 'NOTE THIS IS NOT ALL OF THE CODE GENERATED, JUST SAMPLE WRAPPERS Public Class CustomersRow Inherits DataRow Private tableCustomers As Customers … Public Overloads Overridable Function AddCustomersRow( _ ByVal columnCustomerID As String, _ ByVal columnCompanyName As String, _ ByVal columnContactName As String, _ ByVal columnContactTitle As String, _ ByVal columnAddress As String, _ ByVal columnCity As String, _ ByVal columnRegion As String, _ ByVal columnPostalCode As String, _ ByVal columnCountry As String, _ ByVal columnPhone As String, _ ByVal columnFax As String) As CustomersRow Dim rowCustomersRow As CustomersRow rowCustomersRow = CType(Me.NewRow,CustomersRow) rowCustomersRow.ItemArray = New [Object]() { columnCustomerID, _ columnCompanyName, _ columnContactName, _ columnContactTitle, _ columnAddress, _ columnCity, _ columnRegion, _ columnPostalCode, _ columnCountry, _ columnPhone, _ columnFax} Me.Rows.Add(rowCustomersRow) Return rowCustomersRow End Function Chapter 13 530 Public Overridable Property Address As String Get Return CType(Me(Me.tableCustomers.AddressColumn),String) End Get Set Me(Me.tableCustomers.AddressColumn) = value End Set End Property The above code illustrates some of the accessors that are generated. These include: MyDataSet.Customers.CustomersRow MyDataSet.Customers.AddCustomersRow([ID],[Name],[etc]) MyDataSet.Customers(0).Address I encourage you to use the generator and explore the code, or use the generator and use statement completion in the Visual Studio editor to explore. These classes can help expose the schemas that you export to other developers, easily and quickly. Summary Throughout this chapter we've examined a series of topics all revolving around XML support within ADO 2.6 and SQL Server 2000. As we've seen, there are quite a few techniques now at our disposal that we can employ to integrate XML into our applications. We've discussed: ❑ How to persist data to files and streams of different sorts. ❑ How to query SQL data without writing any SQL, via the XPath and Mapping Schema techniques. ❑ The technique of writing XML template queries against SQL database. ❑ How we can merge XML data sets with SQL data sets to retrieve and even modify SQL data. We've also been introduced to ADO+. In summary, ADO+ will provide many new ways to communicate with databases and XML, as well as interoperate between them. We barely scratched the surface of ADO+ as well as the new Microsoft.NET framework. Keep up with the latest at http://msdn.microsoft.com. XML is coming from all angles in today's technology surge. These new features and techniques help to prepare us for the next generation of application development as we move to an XML data-centric world. ADO, ADO+, and XML 531 Chapter 13 532 Storing and Retrieving XML in SQL Server 2000 In this Internet world, many of the applications you'll come across will be Web applications that share a common requirement – having HTML pages with dynamic data. We can write ASP (Active Server Pages) applications that retrieve data from the database and do the necessary conversion to display data in those HTML or XML pages, but this requires a certain amount of development work. SQL Server 2000 now includes XML support, which we can use to get these Internet documents in and out of SQL Server 2000 without having to write complex ASP applications. These new XML features allow us to literally view the entire relational database as XML. Thus we can write end-to-end XML applications. A number of XML-related technologies have been introduced to support the storage and retrieval of XML into and out of SQL Server. There are a number of new features on the server side, as well as some on the middle tier. The middle tier features are discussed in the next chapter. In this chapter, we'll concentrate on the server side features. These server side features include: ❑ FOR XML – allows you to retrieve data from SQL Server as an XML ❑ OPENXML – allows you to shred XML documents and store them in relational tables ❑ XML Bulk Load – allows you to bulk load from an XML document into SQL ❑ XML Views – provide an XML view of relational data ❑ XPath query support – allows you to query the XML view ❑ XML Updategrams – allow you to update data in relational tables using the XML views Chapter 14 534 In SQL Server 2000, we can write SELECT queries that return XML instead of the standard rowsets. The new FOR XML clause is used to get the results of a query returned as an XML tree instead of a rowset. These features are built in to the database query processor, which significantly enhances query performance. If we want to update data in SQL Server 2000 from an XML document source, we can do that as well, using extensions to SQL INSERT, UPDATE, and DELETE statements. The new OPENXML functionality generates a rowset view of the XML. This rowset view can then be used in place of a recordset to update database tables. Finally, when we have a large amount of XML that we want to put into SQL Server 2000, we can use the Bulk Load object to add this data for us. There are many cases where we may require data to be returned in an XML format. For example, to his is typically the case if our application uses public XML schemas (such as Microsoft's Biz Talk schemas). There are two main ways in which you can retrieve XML: ❑ Writing SQL queries – with special extensions – directly against SQL Server 2000 tables ❑ Defining XML views of relational data, and retrieving the data as XML using these XML views With both of these methods, the transformation of the relational data into XML is all done for you. All of this makes Web application development much simpler, because you avoid writing complex programs to retrieve and display data. Note that almost all the examples in this chapter use the Northwind database that comes with SQL Server 2000. We assume that you already have some knowledge of SQL. Retrieving XML from SQL Server 2000: FOR XML In relational databases, as you well know, the standard practice employed to query or summarize data is to run queries against that data. In SQL, queries specified against the database return results as a rowset. For example, the following query, ch14_ex01.sql: SELECT CustomerID, ContactName FROM Customers when run against the Northwind database, returns a 2-column rowset of customer IDs and contact names from the Customers table as a result, as shown opposite: Storing and Retrieving XML in SQL Server 2000 535 New SQL Server Query Support SQL Server 2000 provides enhanced query support, in which we can request the result of a SELECT statement be returned as an XML document. To retrieve the result of a SELECT statement as XML, we must specify the FOR XML clause in the SELECT statement, along with one of three modes: RAW, AUTO, or EXPLICIT. For example, if we wanted our previous sample query to return XML instead of a results table, we could append to it as follows: SELECT CustomerID, ContactName FROM Customers FOR XML AUTO This query uses the AUTO mode (which is specified after the FOR XML clause) and returns a long list of customer information as XML, of which the following is a fragment: <Customers CustomerID="ALFKI" ContactName="Maria Anders" /> <Customers CustomerID="ANATR" ContactName="Ana Trujillo" /> We've added formatting to the output XML in this chapter to make it more readable – the result you obtain will be a continuous string. However, note that while the FOR XML mode can be used in a SELECT statement, it cannot be used in nested SELECTs, such as: SELECT * FROM Table1 WHERE … = SELECT * FROM Table2 FOR XML AUTO Let's briefly summarize what each of the three FOR XML modes does, before going on to look at each one in more detail: ❑ The RAW mode produces attribute-centric XML with a flat structure. Each row in the table is represented in a generic tag called ROW, with the columns represented as attributes. The RAW mode is the easiest of the three modes to administer, although it does limit you to this structure. Chapter 14 536 ❑ The AUTO mode produces XML where the hierarchy of elements in the resulting XML is determined by the order of columns in the SELECT statements, so you have limited control over the shape of the XML produced. This mode provides a compromise between control and complexity. ❑ The EXPLICIT mode allows the user to have total control over the shape of the resulting XML. However, the downside is that it is more complicated to administer than the other modes. FOR XML: General Syntax Here is the syntax for the FOR XML clause that is specified in the SELECT statement: FOR XML xml_mode [,XMLDATA], [,ELEMENTS], [BINARY BASE64] ❑ If the optional XMLDATA option is specified, an XML-Data schema for the resulting XML is returned as part of the result. The schema is prepended to the XML. ❑ The ELEMENTS option is specified if an element-centric document is to be returned in which the column values are returned as sub-elements. By default the column values map to the element attributes in XML. The ELEMENTS option only applies in AUTO mode. We can request an element-centric document in EXPLICIT mode, but the ELEMENTS option isn't the way to do it. ❑ If the BINARY Base64 option is specified in the FOR XML clause, any binary data returned (for example, from a SQL Server IMAGE-type field) is represented in base64-encoded format. To retrieve binary data using the RAW and EXPLICIT modes, this option must be specified. It is not required when AUTO mode is specified, but without it, the AUTO mode returns binary data as a URL reference by default. For example this query retrieves employee ID and photo (an image type column) for employee 1: SELECT EmployeeID, Photo FROM Employees WHERE EmployeeID=1 FOR XML AUTO The query returns this result: <Employees EmployeeID="1" Photo="dbobject/Employees[@EmployeeID='1']/@Photo"/> Notice the URL reference to the Photo column returned. This URL reference can be used later to retrieve the employee photo. The dbobject is a virtual name of dbobject type. And the Employees[@EmployeeID='1']/@Photo is an XPath expression that is used to retrieve the image. The virtual names are created as part of creating a virtual directory for SQL Server. Now let's look at each of the modes in detail. [...]... type="OrderID"/> 5 37 Chapter 14 You may need to change... special stored procedures to do this: ❑ sp _xml_ preparedocument ❑ sp _xml_ removedocument 5 57 Chapter 14 Before OPENXML can access the XML document, the sp _xml_ preparedocument stored procedure must be called to generate an in-memory DOM representation of the XML The stored procedure returns a document handle of this in-memory DOM tree This document handle is passed to OPENXML, which then generates the rowset... @doc varchar(1000) Source XML document SET @doc ='Copy XML document here ' Create an internal representation of the XML document EXEC sp _xml_ preparedocument @hdoc OUTPUT, @doc Your SELECT statement goes here EXEC sp _xml_ removedocument @hdoc 558 Storing and Retrieving XML in SQL Server 2000 Understanding OPENXML Having seen how we create the DOM representation that lets OPENXML generate a rowset, let's... OPENXML … … Thus, if we want to apply updates to a database table from data that is XML, then OPENXML can be used with INSERT, UPDATE, or DELETE statements to apply necessary updates from the XML data For example: INSERT INTO Customers SELECT * FROM OPENXML … WHERE Creating the In-Memory Representation of the Document As we said, we need to create a DOM tree that represents the XML, so that OPENXML... spare XML (also referred to as unconsumed XML) into its own column (referred to as an overflow column) for safe keeping, until it is needed again This can be done as part of the functionality of OPENXML, discussed later in the chapter The xmltext directive is used to identify a database column as an overflow column, pull out all of the XML contained within this column, and put it back into an XML format... [Customer!1!CustomerID!id], [Order!2!OrderID] FOR XML EXPLICIT, XMLDATA Here, the id and idref directives are specified in the first SELECT clause The query also requests an XDR schema by specifying the XMLDATA option, so that we can see the changes in the schema due to the id and idref directives This is the partial result ... complex – writing these queries to produce complex XML documents can be a challenge However, there is an alternative We can create XML Views of the relational data, and specify XPath queries against these views In SQL Server 2000, we can create XML views using XML- Data reduced language (a subset of XMLData schema language) XPath queries generate FOR XML EXPLICIT queries to retrieve data from the database... data is an XML document, and we want to INSERT this data into the database, UPDATE the existing relational data from the source XML document, or DELETE existing records in the tables based on the data in the XML document, then somehow we need to create a rowset from the XML data and pass it to the INSERT, UPDATE, or DELETE statement 556 Storing and Retrieving XML in SQL Server 2000 OPENXML provides... functionality The OPENXML function in SQL Server 2000 is a rowset provider, which means it creates a rowset view of an XML document Since a rowset is like a table, it can be used in place of a table or relational view in SELECT queries Thus, the OPENXML feature in SQL Server 2000 allows you to store data from XML documents or document fragments in database tables OPENXML is one way of storing XML in the database... way of storing XML in the database using SQL In addition, you can store XML using XML updategrams, which are discussed in the next chapter To do this there are a number of steps that take place, we need to: ❑ Create an in-memory DOM representation of the XML document ❑ Use OPENXML to create a rowset view of this XML As part of OPENXML, specify an XPath expression to retrieve the desired elements ❑ Pass . Studio 7 introduces a new XML Schema designer used to create XML schema and data. The tool allows for many scenarios, including mapping databases to XML schema, creating XML schema from databases, . the SELECT statement: FOR XML xml_mode [,XMLDATA], [,ELEMENTS], [BINARY BASE64] ❑ If the optional XMLDATA option is specified, an XML- Data schema for the resulting XML is returned as part of. allows you to bulk load from an XML document into SQL ❑ XML Views – provide an XML view of relational data ❑ XPath query support – allows you to query the XML view ❑ XML Updategrams – allow you to

Ngày đăng: 13/08/2014, 12:21

TỪ KHÓA LIÊN QUAN