如何将SQL server数据库模式和所有表中的数据转换为XML形式

本文关键字:数据 转换 形式 XML SQL server 数据库 模式 | 更新日期: 2023-09-27 18:07:12

我想将数据库中的数据转换为XML形式使用c#,我已经将其转换为XML形式,但它没有给我关于模式

中的主键和外键的信息。

我的代码是

 string[] b = GetAllTables();//array that gives me the name of tables
  foreach (string c in b)
    {
      string sqlText = "SELECT * FROM  " + c;
      SqlDataAdapter da = new SqlDataAdapter(sqlText, myCon);
      da.SelectCommand.CommandType = CommandType.Text;
      da.Fill(customer,c);
      foreach (DataTable dt1 in customer.Tables)
        {
          for (int curRow = 0; curRow < dt1.Rows.Count; curRow++)
          {
            for (int curCol = 0; curCol < dt1.Columns.Count; curCol++)
            {  
              dt1.Rows[curRow][curCol].ToString();  
              // System.IO.StreamWriter xmlSW = new System.IO.StreamWriter(@"D:'Customer.xml");
            }
          }
        }
     }
  string xmlDS = customer.GetXml();
  customer.WriteXml(xmlSW, XmlWriteMode.WriteSchema);
  MessageBox.Show("successfully writed");
  xmlSW.Close();

如何将SQL server数据库模式和所有表中的数据转换为XML形式

你可以尝试使用XElement类并像下面这样设置xml文件的属性(只是一个例子):SqlConnection thisConnection = new SqlConnection(@"数据源= 3bdalla - pc;初始目录=XMLC;集成安全=真;池=假;");

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

然后将数据库的列标签及其名称放入xml文件中。

问候,

Otacon .