Monday, June 28, 2010

More SQL XML, Making It Easier...

Well, I've previously used stored procedures to execute SQLXML queries, pulling the XML out using a standard SqlDataAdapter and concatenate the returned rows via a StringBuilder.

There is, however, a different mechanism to use:
  • It does throw a large number of exceptions, so your code needs to be clean.
  • You will need to remove System.Data.SqlClient because the new DLL replaces the namespaces found in the original DLL.
Example code:

using Microsoft.Data.SqlXml;

string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlXmlCommand queryCommand = new SqlXmlCommand(connectionString);
queryCommand.CommandText = @"SQL * FROM something FOR XML";

SqlXmlParameter queryParameter = queryCommand.CreateParameter();
queryParameter.Value = 2860;
queryCommand.RootTag = "root";
queryCommand.XslPath = Server.MapPath("~/XSLT/formatter.xslt");
queryCommand.ClientSideXml = true;
XmlDocument xmlChartData = new XmlDocument();
xmlChartData.Load(queryCommand.ExecuteXmlReader());


No comments:

Post a Comment