将SQL转换为LINQ到XML

4
我正在编写以下代码将SQL转换为LINQ,然后转换为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);

表的名称是“XMLC”,我想引用它。我该怎么做? 当我直接输入它的名称时,VS会报错。当我说thisConnection.XMLC时,它也不起作用。
1个回答

1

听起来你想使用Linq-to-Sql。在这种情况下,你需要创建一个数据上下文。有很多关于此的教程。

然而,你不一定需要使用linq to sql。你的数据源可以是任何可枚举的对象。例如,一个DataReader:

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"])));

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接