当前位置:编程学习 > C#/ASP.NET >>

请帮看下这个sql for xml语句在.net中执行结果变了

sql原语句是:
select name as "Schema/@name" ,
    (select name as "TableName/@name"from sys.tables 
       where a.schema_id=schema_id
      for xml path(''),type)as "Schema/*"
from sys.schemas AS a
where a.name in (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES)
for xml path(''),root('Database')
输出结果为一个xml文件,具有许多同名节点(在同一父节点下)


在.net中用了转义符,变成一下语句来使用
select name as \"Schema\\@name\" ,(select name as \"TableName\\@name\"from sys.tables  where a.schema_id=schema_id for xml path(''),type)as \"Schema\\*\" from sys.schemas AS a where a.name in (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES) for xml path(''),root('DBInformation')

用executexmlreader读出来的全变了,结果是
<row>
<name>dbo</name>
</row> --------------------编程问答-------------------- 你直接SQL 到XML有没问题? --------------------编程问答--------------------
引用 1 楼 cjh200102 的回复:
你直接SQL 到XML有没问题?

直接的话是没问题的,给的结果是想要的。
但是到了.net里就不对了。 --------------------编程问答--------------------
string cmd="select name as \"Schema\\@name\" ,(select name as \"TableName\\@name\"from sys.tables where a.schema_id=schema_id for xml path(''),type)as \"Schema\\*\" from sys.schemas AS a where a.name in (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES) for xml path(''),root('DBInformation')"

这样赋值给一个字符变量是错的么?
怎么执行的结果不对?? --------------------编程问答-------------------- 加下断点,跟踪调试,运行到这一句的时候把cmd获得的值取出来,复制到数据库中,看看哪个地方的符号多了还是少了。~

不然这么找很难~~ --------------------编程问答--------------------
string cmd="select name as \"Schema\\@name\" ,(select name as \"TableName\\@name\"from sys.tables where a.schema_id=schema_id for xml path(''),type)as \"Schema\\*\" from sys.schemas AS a where a.name in (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES) for xml path(''),root('DBInformation')"




现在这个查询出来的是正确结果了。
但是又出来个新的问题:我是用executereader读取。然后在使用ExecuteReader.Reader()时,要么只有第一个节点,要么就提示
xmlexception \0”为意外标记。预期标记为“"”或“'”
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,