开发者

Converting SQL to LINQ to XML

开发者 https://www.devze.com 2022-12-26 23:38 出处:网络
I\'m writing 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;Pooli

I'm writing 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 ?????? 
        se开发者_开发百科lect 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 that? When I put its name directly VS gives an error. Also when I say thisConnection.XMLC it doesn't work.


It sounds like you want to use Linq-to-Sql. In that case you'll need to create a data context. There are plenty of tutorials on that.

You don't need to use linq to sql, however. Your source can be any enumerable. A 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"])));

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号