Generate XML with data from SQL Server
6735 단어 SQL Server
//-----------------------------------------------------------------------
// 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
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SQL Server의 여러 데이터베이스 통합 백업이 문서는 거울입니다.최신 정보를 보려면 다음 Qita 문서를 확인하십시오. SQL Server의 여러 데이터베이스를 통합 백업하는 데 사용되는 T-SQL입니다.마이그레이션할 때 데이터베이스를 통일적으로 백업할 때 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.