Read multiple tables with relations with t-sql into DataSet

大憨熊 提交于 2019-12-13 03:05:50

问题


Is it possible to select data with one t-sql statement from multiple tables (join) into a string representation and directly insert this text into a data set (DataSet.ReadXml(...)).

I need to preserve the relations, of course.

I have seen some complex examples using FOR XML, but is there a simple way?


回答1:


well, I did it myself:

SQL = "SELECT table1.x, table2.z FROM table1 
       INNER JOIN table2 ON table1.ID = table2.SubID
       FOR XML AUTO, XMLDATA";

and

DataSet data = new DataSet();
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
data.ReadXml(((System.Data.SqlClient.SqlCommand)cmd).ExecuteXmlReader(),
               xmlReadMode.Fragment);                        

that's it



来源:https://stackoverflow.com/questions/5326703/read-multiple-tables-with-relations-with-t-sql-into-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!