我正在编写以下代码将 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
时,它不起作用。
最佳答案
听起来您想使用 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"])));
}
关于c# - 将 SQL 转换为 LINQ to XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2656279/