return value Auto Mode XML
I am trying to dynamically write some xml for a webservice. I have recently been shown how to use the FOR XML AUTO clause for my sql query, but I am not sure If one I am writing the query properly and on top of this I am not sure how to return the document values
using (SqlConnection oCn = new SqlConnection())
{
oCn.Connecti开发者_运维百科onString = @"server=sql-server\cos;integrated security=SSPI;database=daas5";
oCn.Open();
// Create a SQL command object.
string strSQL = "SELECT * FROM dd615.musicdetails WHERE artistname LIKE '%" +
searchTerm + "%' OR recordname LIKE '%" + searchTerm + "%'
OR recordtype LIKE '%" + searchTerm + "%'
OR format LIKE '%" + searchTerm + "%'
FOR XML AUTO, ELEMENTS, ROOT('musicInformation') ";
SqlCommand myCommand = new SqlCommand(strSQL, oCn);
}
any help would be greatly appreciated
Your SQL seems fine so far - the best bet would be to use the .ExecuteXmlReader
on your SqlCommand
object.
XmlReader reader = myCommand.ExecuteXmlReader();
Once you have that, you can easily load this into an XDocument
XDocument doc = XDocument.Load(rdr);
or an XmlDocument
XmlDocument xdoc = new XmlDocument();
xdoc.Load(rdr);
so pick whichever works for you!
精彩评论