One. Foreword: XML as the cornerstone of the web service, its importance is naturally self-evident, it is increasingly valued by developers. At the same time, with the launch of various types of emerging development tools, the access mechanism of XML data has become more flexible. The .NET framework provides developers with several new XML data access mechanisms, each mechanism provides different XML data access support. Therefore, for developers, choose the right and suitable XML data access mechanism becomes quite important, which affects the complexity of project development and the overall efficiency of the application. Generally speaking, .NET framework provides developers with a data set XML data access mechanism in ADO.NET and two new mechanisms of the SQLDataReader XML data access mechanism. For convenience, we may wish to refer to the previous mechanism as an ADO.NET mechanism, and the latter is a SqlDataReader mechanism. This article, I mainly introduce these two mechanisms, and I will also introduce two more traditional XML data access mechanisms that compare SQLXML and ADO 2.6, and make a comparison in the end of their performance so that the readers are more clear in specific Which mechanism should be chosen in the application. The instance programs of this article will use the Northwind database in SQL Server, and through access to this database we can get the corresponding XML data and perform performance comparisons and analysis on different access mechanisms. two. Introduction to various mechanisms: ADO.NET is an Ado upgrade version, which provides us with a new data access mechanism, which greatly simplifies the workload of developers while improving data access efficiency, so proficiency and use ADO.NET Development is an indispensable basic ability of .NET developers. ADO.NET provides developers with two data access modes: a connection mode, another is a non-connection mode (disconnected). The previous mode is the same as the traditional ADO mode, and the latter mode is also recommended for .NET is also recommended for .NET, the core part of this mode is the data set object in ADO.NET. The class of dataset objects is located in System.Data namespace, which has a considerable support for XML. When operating in the non-connection mode, the data set object can store the data in the main memory in XML and provide user operation, so it can transform the relational data into hierarchical in a simple method. The XML data of the pattern is the ADO.NET mechanism mentioned earlier. Below, I will introduce the specific method of XML data access under this mechanism by establishing an instance program. First create a C # item of a console application, which mainly shows the operational efficiency of different XML data access mechanisms. At the same time, add a new class, the file name may be named DBXML.CS, which contains several implementation methods of the XML data access mechanism, which are: ExecuteSQLDataReaderselect, and ExecuteSQLXMLSELECT, represents ADO.NET mechanism, SqlDataReader Mechanism and SQLXML mechanism, while ADO 2.6 mechanism will be introduced later. Then add the necessary namespace for this class.
Using system;
Using system.data;
Using system.data.sqlclient;
Using system.text;
USING SYSTEM.XML;
Using microsoft.data.sqlxml;
Among them, for Microsoft.Data.sqlxml namespace needs to add references to Microsoft.Data.SQLXML components, and this component is after installation of SQLXML 3.0, the addition method is shown in Figure 1:
figure 1
Now add an ExecuteAdNetSelect () method to this class. This method performs the SELECT query operation of the database. The specific implementation is as follows: Public String ExecuteadNetSelect (String Customerid,
String connectionString)
{
// Create a database connection object
SqlConnection MyConnection = New SqlConnection (Connectionstring);
// Create a data adapter object
SqldataAdapter MysqldataAdapter1 = New SqlDataAdapter ("SELECT *
From customers where customerid = @customerid ", myconnection);
/ / Add parameters for its parameter set
mysqldataadapter1.selectCommand.Parameters.add ("@ Customerid",
Sqldbtype.char, 5, "customerid");
MySQLDataAdapter1.selectCommand.Parameters ["@ Customerid"].
Value = CustomerId;
// Create a dataset object
Dataset mydataset = new dataset ();
// Use the Fill method of the data set object to populate the data set object
MySQLDataAdapter1.fill (MyDataSet, "Customers");
/ / Turn off the database connection
MyConnection.Close ();
/ / Returns the XML form of data in the data set object
Return mydataset.getxml ();
}
The above ExecuteadONETSELECT () method first creates a SQLConnection object based on the incoming database connection string and then creates a SQLDataAdapter object and passes a SELECT statement and the SQLConnection object above to its constructor. A user ID parameter is included in the SELECT statement, so you must add this parameter to the SELECTCOMMAND parameter set of the SqlDataAdapter object and assign the parameter. The next two steps created a new DataSet object and fill the DataSet object with the SQLDataAdapter object above, although it is simple, but they have completed most of the work. Finally, turn off the database connection and return data in the dataset object in XML. Next, add an ExecuteSQLDataReaderselect () method for this class, which is as follows:
Public String ExecuteSqldataReaderselect (String Customerid,
String connectionString)
{
SqlDataReader myDataReader = null;
// Create a database connection object
SqlConnection mysqlconnection = new SQLCONNECTION
Connectionstring;
/ / Create a database command object and use for XML clause in the SQL statement
SQLCommand mysqlcommand = new sqlcommand ("SELECT * FROMERS
WHERE CUSTOMERID = @customerid for xml raw ", mysqlconnection);
MySQLCommand.commandType = commandType.text; / / Add parameters for its parameter set
MySQLCommand.Parameters.Add ("@ Customerid", SqldbType.char, 5,
Customerid ");
MySQLCommand.Parameters ["@ Customerid"]. Value = CustomerId;
mysqlconnection.open ();
// Execute the ExecuteReader operation of the database command object to obtain a data reader object
MyDataReader = mysqlCommand.executeReader (Commandbehavior).
CloseConnection;
// Create a StringBuilder object to construct an XML string
StringBuilder XML = New StringBuilder (8192);
/ / Continuously get XML data from the data reader and add to the StringBuilder object
While (MyDataReader.Read ())
{
IF (! myDataReader.Indbnull (0))
XML.Append (myDataReader.getstring (0));
}
MyDataReader.Close ();
MySQLConnection.Close ();
/ / Return to the string of XML data
Return Xml.toTostring ();
}
The SQLDataReader class provides us with read-only, forward data access, which makes it the fastest way to access the SQL Server database. However, the SQLDataReader class does not directly support xml type data, you must manually write code to convert the data into XML data or return the XML type by running the FOR XML query the apartment. Here, we will use the For XML RAW clause to get XML data. The above ExecuteSqldataReaderselect () method first declares a SqlDataReader object, and then creates a SQLConnection object through the database connection string parameter and creates a SQLCommand object based on the SELECT statement with the for XML RAW clause and the SQLConnection object above. Thereafter, the method adds parameters to the parameter set of SQLCommand and assigns a value for the user ID parameter. Once the SQLConnection connection is turned, the method assigns the return value of the EXECUTEREADER () method of the SQLCommand object to the SqlDataReader object and reads the XML data in the SqlDataReader object through a StringBuilder object. Finally, the method closes the SqlDataReader object and the SQLConnection object and returns XML data in the form of a string. Finally, add an ExecuteSQLXMLSELECT () method for this class, which is as follows:
Public String ExecuteSQLXMLSELECT (String Customerid, String
Connectionstring, Bool Clientside
{
// Create a SQLXMLCOMMAND object
SQLXMLCOMMAND CMD = New SQLXMLCOMMAND (Connectionstring);
CMD.Roottag = "Customers";
Cmd.clientsidexml = clientside;
CMD.Commandtext = "SELECT * from customers where customerid = '" Customerid "' for XML RAW";
// Execute the ExecuteExmlReader operation of the SQLXMLCommand object to get an XMLReader object
XmlReader XR = cmd.executexmlreader ();
// Create an XMLDocument object
XmlDocument xd = new xmldocument ();
// Use its Load method to load the acquired XML data into a DOM and return the form of XML data
XD.LOAD (XR);
Return XD.outerxml
}
SQLXML hosted classes are part of the SQLXML 3.0 package, and the SQLXML 3.0 package extends the XML function of SQL Server 2000. The SQLXML hosted class is the native .NET class that provides functions of accessing XML data by programming methods. The above ExecuteSQLXMLSELECT () method first creates a SQLXMLCOMMAND object with a database connection string. At the same time, XML data acquired by the for XML clause is often just an XML fragment, not a well-final XML data, to make XML a good structure, you must set the roottag property of the SQLXMLCommand object. In this example, we set this property to "Customers". Previous versions of SQLXML packets generate XML data on the server and then pass to the client. Thus, because the XML data stream returned by the server is far greater than the original binary format data stream, an scalability issue has occurred. The 3.0 version of the SQLXML package allows the server to pass data to the client in binary format, and then further transforming data into an XML format in the client, which solves the extensibility problem existing in the original version. The method of implementing this feature in a version 3.0 version is simple, just set the ClientSideXml property of the SQLXMLCommand object to true. In this way, the program will continue to use the SELECT statement with the for XML clause, but the managed class will peeled the FOR XML clause before sending the SQL statement to SQL Server. The database will not find the for XML clause, so the data returned from the server to the client is also the binary format. After the client gets the data returned from the server, the managed class will further convert the data to an XML format. In the above program, we get an XMLReader object by executing the ExecuteExmander () method of the SQLXMLCommand object, and use it to fill an XMLDocument object, and finally returns the OuterXML attribute of the XMLDocument object to the call. Above, I introduced you three different ways to add ADO.NET mechanism, SqlDataReader mechanism, and SQLXML mechanism. The following is a schematic diagram of a result of these three different methods. From the result of the results, readers can see that The method has the highest efficiency. Bamboo
figure 2
The end to introduce the ADO 2.6 mechanism, the mechanism is implemented in VB 6.0. Let's create an ActiveX DLL project called DBXMLVS6, and then modify the class1 to ADO26, then add a reference to ActiveX Data Objects 2.6. Once finished, add an ExecuteSelect () method for this class:
Public Function Executeeselectr
Customerid As string, Connectionstring As String)
As string
SET CONN = New Adodb.Connection
Conn.connectionstring = connectionstring
Conn.open
Set cmd = new adoDb.command
cmd.activeConnection = conn
cmd.commandtext = "SELECT * FROMERS
WHERE CUSTOMERID = '"& customerID &"' "
SET RS = cmd.execute
Set adoStream = new adodb.stream
AdoStream.Type = adtypetext
AdoStream.open
rs.save adostream, AdpersistXML
'Return to XML string
Executeselectr = AdoStream.readText ()
END FUNCTION
As with other instances, the above program creates and opens a database connection. Then create a Command object and set its ActiveConnection property to the Connection object above, and the SELECT statement used in the program is the same in the previous instance. Next, the program passes the return value of the EXECUTE () method of the Command object to a Recordset object. Then, the program creates an ADO Stream object and sets its Type property to AdTytext to open it. Once the flow object is opened, the Save () method of the Recordset object can be called in XML format. Finally, the program returns XML data to the caller by retaText () method of the Stream object. Combined with the four different XML data access mechanisms described above, I use ASP and ASP.NET pages to test the performance and scalability of its XML data accesses in the way of calling components. The result is obtained as shown below. Analysis results for readers' reference.
image 3
three. Summary: I introduce you to the four different mechanisms of XML data accesses under the .NET framework. With Microsoft's continuous addition of new features and improve performance, XML data access will continue to continue. At the same time, in view of the new version of the SQL Server database server adds a lot of XML native support, we can look forward to the great development of XML data access in the near future.