Generate XML with data from SQL Server

Source: Internet
Author: 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
// Implied warranties of merchantability and/or fitness for
// Particle 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 MERs 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 MERs", 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
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.