Created
March 9, 2012 03:30
-
-
Save lmatt-bit/2004868 to your computer and use it in GitHub Desktop.
SQL to XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| XElement root = new XElement("Orders"); | |
| using (SqlConnection con = new SqlConnection("data source=LMATT-PC\\SQLEXPRESS;integrated security=SSPI")) | |
| { | |
| con.Open(); | |
| using(SqlCommand command = new SqlCommand("SELECT top 50 * from Spt_values", con)) | |
| { | |
| SqlDataReader reader = command.ExecuteReader(); | |
| while(reader.Read()) | |
| { | |
| root.AddFirst( | |
| new XElement("Order", | |
| from i in Enumerable.Range(0, reader.FieldCount) | |
| select | |
| new XElement(reader.GetName(i), reader.GetValue(i)) | |
| ) | |
| ); | |
| } | |
| root.Save(Console.Out); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment