2013-05-27 34 views
3

生成XML我在SQL Server中生成一個XML輸出的查詢。使用C#從SQL Server數據

我想生產使用C#相同的結果。可能嗎??

查詢是

select T1_1.HomeID as [@HomeID], 
(
    select T1_2.DayID as [@ID], 
    (select T2.RndString+' '+left(T1_3.TimeValue, 5) as '*' 
    from TB1 as T1_3 
    inner join TB2 as T2 on T1_3.DayID = T2.DayType 
    and T1_3.TimeCode = T2.StringCode 
    where T1_2.HomeID = T1_3.HomeID 
    and T1_2.DayID = T1_3.DayID 
    order by T2.StringCode 
    for xml path('String'), type) 
    from TB1 as T1_2 
    where T1_2.HomeID = T1_1.HomeID 
    group by T1_2.DayID,T1_2.HomeID 
    order by T1_2.DayID 
    for xml path('Day'), type) 
    from TB1 as T1_1 
    group by T1_1.HomeID 
    order by T1_1.HomeID 
    for xml path('Person'), root('Persons') 

有關此參考我先前的職位的進一步細節。 Producing XML from Multiple Tables in SQL Server

我在C#極差。各種初學者。這裏需要一些幫助。

我使用的代碼...

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Windows.Forms; 
    using System.Text; 
    using System.Data; 
    using System.Data.SqlClient; 

    namespace SQL__ 
    { 
     static class Program 
     { 
      /// <summary> 
      /// The main entry point for the application. 
      /// </summary> 
      [STAThread] 
      static void Main() 
      { 
       // Create a String to hold the database connection string. 
       // NOTE: Put in a real database connection string here or runtime won't work 
       string sdwConnectionString = @"Data Source=IE1ADTBD5ZL1S\;Initial Catalog=RecommendEngine;Integrated Security=True"; 

       // Create a connection 
       SqlConnection sdwDBConnection = new SqlConnection(sdwConnectionString); 

       // Open the connection 
       sdwDBConnection.Open(); 

       // To generate XML File using C# from SQL Server Data 

       using (SqlDataAdapter da = new SqlDataAdapter()) 
       { 
        da.SelectCommand.CommandText = @"select T1_1.HomeID as [@HomeID], 
                (
                 select T1_2.DayID as [@ID], 
                   (
                   select T2.RndString+' '+left(T1_3.TimeValue, 5) as '*' 
                   from TB1 as T1_3 
                   inner join TB2 as T2 
                    on T1_3.DayID = T2.DayType and 
                     T1_3.TimeCode = T2.StringCode 
                   where T1_2.HomeID = T1_3.HomeID and 
                    T1_2.DayID = T1_3.DayID 
                   order by T2.StringCode 
                   for xml path('String'), type 
                  ) 
                 from TB1 as T1_2 
                 where T1_2.HomeID = T1_1.HomeID 
                 group by T1_2.DayID, 
                   T1_2.HomeID 
                 order by T1_2.DayID 
                 for xml path('Day'), type 
                ) 
                from TB1 as T1_1 
                group by T1_1.HomeID 
                order by T1_1.HomeID 
                for xml path('Person'), root('Persons')"; 
        da.SelectCommand.Connection = new SqlConnection("sdwDBConnection"); 

        string xml = ""; 
        using (DataSet ds = new DataSet()) 
        { 
         da.SelectCommand.Connection.Open(); 
         da.Fill(ds); 
         da.SelectCommand.Connection.Close(); 

         if (ds != null && ds.Tables.Count > 0) 
          xml = ds.GetXml(); 
        } 
       } 

       // Close the connection 
       sdwDBConnection.Close(); 


      } 
     } 
    } 

回答

2

您可以使用DataTable.WriteXML()DataTable.WriteXmlSchema()方法來生成XML的查詢。

SqlCommand cmd = new SqlCommand("Your Command", new SqlConnection("Connection String")); 
DataTable dt = new DataTable(); 
new SqlDataAdapter(cmd).Fill(dt); 

dt.TableName = "Your Table Name"; 
dt.WriteXml("File Address"); 
dt.WriteXmlSchema("Schema File Address"); 
+0

我得到一個InvalidOperationException在使用一段代碼。它讀取「無法序列化DataTable,DataTable名稱未設置。」 你可以指定我應該填寫的WriteXml() – SarangArd

+0

我得到了解決:) – SarangArd

+0

你應該設置'dt.TableName'屬性,我忘了它。對不起:) – Mojtaba

3

嘗試和真實:

using (SqlDataAdapter da = new SqlDataAdapter()) 
{ 
    da.SelectCommand.CommandText = "you sql command"; 
    da.SelectCommand.Connection = new SqlConnection("your connection"); 

    string xml = ""; 
    using (DataSet ds = new DataSet()) 
    { 
     da.SelectCommand.Connection.Open(); 
     da.Fill(ds); 
     da.SelectCommand.Connection.Close(); 

     if(ds != null && ds.Tables.Count > 0)  
      xml = ds.GetXml(); 
    } 
} 
+0

我得到一個錯誤,「NULLREFERENCEEXCEPTION was unhandled。」我不完全理解它。 – SarangArd

+0

看到我編輯的代碼,或許你的數據集是空 – Ted

+0

的空引用異常朝「大」指點 – SarangArd

相關問題