Convert SQL to LINQ to XML

I write the following code to convert SQL to LINQ, and then to XML:

SqlConnection thisConnection = new SqlConnection(@"Data Source=3BDALLAH-PC;Initial Catalog=XMLC;Integrated Security=True;Pooling=False;"); thisConnection.Open(); XElement eventsGive = new XElement("data", from c in ?????? select new XElement("event", new XAttribute("start", c.start), new XAttribute("end",c.eend), new XAttribute("title",c.title), new XAttribute("Color",c.Color), new XAttribute("link",c.link))); Console.WriteLine(eventsGive); 

The name of the table is "XMLC", and I want to refer to it. How can i do this? When I put my name directly, VS gives an error. Also, when I say thisConnection.XMLC , this does not work.

+4
source share
1 answer

Looks like you want to use Linq-to-Sql. In this case, you need to create a data context. There are many lessons to this.

However, you do not need to use linq for sql. Your source can be any enumerated. DataReader, for example:

 using (DbDataReader rdr = cmd.ExecuteReader()) { from c in rdr.Cast<DbDataRecord>() select new XElement("event", new XAttribute("start", c["start"]), new XAttribute("end",c["eend"]), new XAttribute("title",c["title"]), new XAttribute("Color",c["Color"]), new XAttribute("link",c["link"]))); } 
+1
source

Source: https://habr.com/ru/post/1307191/


All Articles