2013-04-03 40 views
2

我想用xml格式将数据库中的数据转换为 使用c#我已经在ino xml格式中转换,但是它没有提供关于在架构主键和外键如何将sql服务器数据库模式加上所有表中的数据转换为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(); 
+0

在MS SQL Server中,您可以在查询结尾处添加“SELECT * FROM Table FOR XML AUTO”以及“SELECT * FROM Table FOR XML RAW”。试试你的SQL编辑器,看看结果是否有助于你的最终目标。 – 2013-04-03 18:15:11

回答

0

您可以尝试使用的XElement类,并设置XML文件的属性,如下面的(只是一个例子): 的SqlConnection thisConnection =新的SqlConnection (@“数据源= 3BDALLAH-PC;初始目录= XMLC;集成安全性=真;池= Fals È;“); 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文件中。

Regards,

Otacon。

相关问题