XML解析:第1行,第7个字符,预期分号

时间:2013-08-28 09:02:57

标签: c# sql sql-server xml sqlxml

在C#中运行此XML SQL时发生错误。缺什么?它在SQL Server管理工作室中运行良好,但在c#中没有。

protected DataTable ServiceCodes(string orderColumn = default(string), bool orderAscending = true)
{
    string sql = @" SELECT DISTINCT(Split.a.value('.', 'VARCHAR(100)')) AS Data  
                             FROM  
                             (
                                 SELECT CAST('<M>' + REPLACE(Text09, ',', '</M><M>') + '</M>' AS XML) AS Data  
                                 FROM  Assets
                                 WHERE Category = 'Service'
                             ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a) ORDER BY Data";

    return DatabaseConnection.ExecuteDataTable(sql, 120000);
}

1 个答案:

答案 0 :(得分:4)

我认为您Text09列中有一些特殊字符。试试这个:

SELECT DISTINCT(Split.a.value('.', 'VARCHAR(100)')) AS Data  
FROM  
(
 SELECT CAST('<M>' + replace((select Text09 + '' for xml path('')), ',', '</M><M>') + '</M>' AS XML) AS Data  
 FROM  Assets
 WHERE Category = 'Service'
) AS A CROSS APPLY Data.nodes ('/M') AS Split(a) ORDER BY Data

请参阅sql fiddle demo