Sunday, March 14, 2010

C# - read XML from SQL2005

Below is an exmaple to read XML content from SQL2005. Use code as below, otherwise, the returned XML may be truncated with size of 2K bytyes

{
....
System.Xml.XmlReader reader = null;

try
{
conn = new SqlConnection(g_strConnDB);
conn.Open();
cmd = new SqlCommand(strCmd, conn);

strCmd = "select *** from *** FOR XML RAW";
cmd.CommandText = strCmd;
reader = cmd.ExecuteXmlReader();
reader.Read();

strResult = "";
while (reader.ReadState != ReadState.EndOfFile)
{
strResult += reader.ReadOuterXml();
}
reader.Close();

....
}
}

No comments: