Generate XML with data from SQL Server

來源:互聯網
上載者:User

//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET Framework SDK Code Samples.
// http://asp.dotnetheaven.com/
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;
using System.Text;
using System.IO;

namespace Microsoft.Samples.HowTo.ADONET
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class XmlFromSqlSrv : System.Web.UI.Page
{

StringBuilder builder = new StringBuilder();
protected System.Web.UI.HtmlControls.HtmlForm output;


private void Page_Load(object sender, System.EventArgs e)
{
Run();
this.output.InnerHtml = builder.ToString();
}


public void Run()
{

String sConnection = "server=(local)\\SQLExpress;Integrated Security=SSPI;database=northwind";
SqlConnection mySqlConnection = new SqlConnection(sConnection);
SqlCommand mySqlCommand = new SqlCommand("select * from customers FOR XML AUTO, XMLDATA", mySqlConnection);
mySqlCommand.CommandTimeout = 15;

try
{
mySqlConnection.Open();

// Now create the DataSet and fill it with xml data.
DataSet myDataSet1 = new DataSet();
myDataSet1.ReadXml(mySqlCommand.ExecuteXmlReader(), XmlReadMode.Fragment);

// Modify to match the other dataset
myDataSet1.DataSetName = "NewDataSet";


// Get the same data through the provider.
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select * from customers", sConnection);
DataSet myDataSet2 = new DataSet();
mySqlDataAdapter.Fill(myDataSet2);

// Write data to files: data1.xml and data2.xml for comparison.
myDataSet1.WriteXml(Path.GetTempPath() + "data1.xml");
myDataSet2.WriteXml(Path.GetTempPath() + "data2.xml");
builder.Append("Data has been writen to the output files: data1.xml and data2.xml <BR/>");
builder.Append("<BR/>");
builder.Append("********************data1.xml******************** <BR/>");
builder.Append(myDataSet1.GetXml() + "<BR/>");
builder.Append("<BR/>");
builder.Append("********************data2.xml******************** <BR/>");
builder.Append(myDataSet2.GetXml() + "<BR/>");
}
catch (Exception e)
{
builder.Append("<BR/>" + e.ToString());
}
finally
{
mySqlConnection.Close();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.