C#基础:数据库生成XML文件

        添加EntityFramework类库,添加数据库连接,ADO.NET Entity Data Model,在Entity Data Model Wizard中选择一个BookContext(上次创建Book类的数据库)数据库连接,

示例代码如下:

  1. using (var db = new BookContext())
  2.             {
  3.                 var query = from b in db.Books
  4.                             orderby b.Title
  5.                             select b;
  6.                 foreach (var s in query)
  7.                 {
  8.                     XElement bookElement = new XElement("book",
  9.                         new XElement("title",s.Title),
  10.                         new XElement("author",s.Author)
  11.                         ); 
  12.                     Console.WriteLine(bookElement);
  13.                 }
  14.                 Console.Write("Program finished, press Enter/Return to continue:");
  15.                 Console.ReadLine();
  16.             }

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/86551154