2010年8月5日 星期四

ASP.NET[C#] SQL撈資料轉XML格式直接輸出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

public partial class user_group : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs ea)
    {
        string connetionString = "Data Source=*****;Initial Catalog=dbname;User ID=****;Password=****";

        SqlConnection cnn = new SqlConnection(connetionString);

        cnn.Open(); 
        //Console.WriteLine("ServerVersion: {0}", cnn.ServerVersion);
        //Console.WriteLine("State: {0}", cnn.State);

        string sqlGroup = "SELECT DISTINCT [db_owner].[Group].[GroupID],[db_owner].[Group].[name] FROM [db_owner].[Group]";
        SqlCommand sqlGroupCmd = new SqlCommand(sqlGroup, cnn);
        SqlDataAdapter groupAdpt = new SqlDataAdapter(sqlGroupCmd);
        DataTable groupDataTable = new DataTable();
        int noOfGroup = groupAdpt.Fill(groupDataTable);
        //Response.Write(noOfGroup);

        XmlDocument ObjXML = new XmlDocument();
        XmlElement menu = ObjXML.CreateElement("menu");//create xml called 'menu'
        ObjXML.AppendChild(menu);// insert root node (root node without attribute)
        
        //menu.SetAttribute("label", null, "User Group");
        //外圈拿到 Group ID Group Name 
        if (noOfGroup > 0)
        {
            foreach (DataRow groupDr in groupDataTable.Rows)
            {
                String str = groupDr[0].ToString();
                int groupID = int.Parse(str);
                //Response.Write(str);
                
                XmlElement node = ObjXML.CreateElement("node");//create child node
                menu.AppendChild(node);// insert child node
                node.SetAttribute("label", null, groupDr[1].ToString());//set child node attribute Group Name

                //內圈拿到每個group id對應的tracker id, tracker name, 

                string sqlText = "SELECT [db_owner].[GroupTracker].[TrackerID],[db_owner].[TrackerUse].[name] FROM  [db_owner].[GroupTracker],[db_owner].[TrackerUse] WHERE [db_owner].[GroupTracker].[GroupID] = " + str + " AND [db_owner].[GroupTracker].[TrackerID] = [db_owner].[TrackerUse].[TrackerID]";

                //string sqlText = "SELECT [db_owner].[TrackerUse].[TrackerID] FROM [Tracker].[db_owner].[TrackerUse]";

                SqlCommand sqlCmd = new SqlCommand(sqlText, cnn);
                SqlDataAdapter adpt = new SqlDataAdapter(sqlCmd);
                DataTable dataTable = new DataTable();
                // Fill the data table with select statement's query results:
                int recordsAffected = adpt.Fill(dataTable);
                
                //Response.Write(recordsAffected + "
");
                
                if (recordsAffected > 0)
                {
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        XmlElement innernode = ObjXML.CreateElement("node");//create child node
                        node.AppendChild(innernode);// insert child node
                        innernode.SetAttribute("label", null, dr[1].ToString());//set child node attribute
                        innernode.SetAttribute("link", null, dr[0].ToString());//set child node attribute

                    }
                }

            }
            XmlDeclaration xmldecl;
            xmldecl = ObjXML.CreateXmlDeclaration("1.0", null, null);
            xmldecl.Encoding = "utf-8";
            ObjXML.InsertBefore(xmldecl, menu);
            string myxml = ObjXML.InnerXml.ToString();
            Response.Write(myxml);//write xml

            if (cnn.State != ConnectionState.Closed)
            {
                cnn.Close();
            }
        }
        else {
            Response.Write("SQL Empty!!");
        }

        

            
        

    }
}


沒有留言: