使用SQL Server中的xml数据类型创建XDocument

时间:2014-02-12 11:05:52

标签: sql xml linq-to-xml

我有这张桌子: ID | FILE

1 | ROOT>      ELEMENT TYPE =“DATABASE”CNNSTR =“Data Source =(local); Initial Catalog = testForDLL; Integrated Security = SSPI; Persist Security Info = False;” QUERY =“SELECT * FROM TBL1 WHERE id = 1”/>    ROOT>

2 | ROOT      ELEMENT TYPE =“DATABASE”CNNSTR =“Data Source =(local); Initial Catalog = testForDLL; Integrated Security = SSPI; Persist Security Info = False;” QUERY =“SELECT * FROM TBL2 WHERE id = 1”/>    ROOT>

我想从第二列加载值并用它创建一个XDOCUMENT。 到目前为止我有这个代码:

using (SqlConnection conn = new SqlConnection(cnnstr))
            {
                conn.Open();
                SqlCommand com = conn.CreateCommand();

                com.CommandText = "SELECT [FILE] FROM XML_Config WHERE id=" + id;
                XDocument doc2 = new XDocument();

                XmlReader dtRead = com.ExecuteXmlReader();

                if (dtRead.Read())
                    doc2.Load(dtRead); -> This line has an error
            }

问你想要什么,以便帮助我。

1 个答案:

答案 0 :(得分:0)

我想我用这段代码解决了这个问题。

using (SqlConnection conn = new SqlConnection(cnnstr))
            {
                string xmlstr;
                conn.Open();
                SqlCommand com = conn.CreateCommand();

                com.CommandText = "SELECT [FILE] FROM XML_Config WHERE id=" + id;

                SqlDataReader dtRead = com.ExecuteReader();

                while (dtRead.Read())
                    xmlstr= dtRead.GetSqlString(0).ToString();
                XDocument docBD = XDocument.Parse(xmlstr);

                return docBD;
            }

你认为这是最好的解决方案吗?