首页  ·  知识 ·  云计算
从数据库中读取XML数据
无为  http://blog.csdn.net/codeshark/archive/2008/07/09/    编辑:dezai  图片来源:网络
创建过程:创建SQL语句打开数据库连接,并调用ExecuteXmlReader()方法从数据库中读取数据,并返回一个XmlReader对象myxmlReader,然后用While语句循环读取Xm

创建过程:创建SQL语句打开数据库连接,并调用ExecuteXmlReader()方法从数据库中读取数据,并返回一个XmlReader对象myxmlReader,然后用While语句循环读取XmlReader对象myxmlReader中的XML片段,并添加XML文件的起始、结尾标志,构建一个XML文件,最后把该文件输出到页面ReaderXml.aspx上面。

ReaderXml.aspx代码如下:

view plaincopy to clipboardprint?
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="ReaderXml.aspx.cs" Inherits="_Default" %>  
 
 
 
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 
 
 
http://www.w3.org/1999/xhtml" >  
 
  
 
    从数据库中读取XML数据  
 
  
 
  
 
      
 
   

  
 
      
 
   
  
 
      
 
  
 
  
ReaderXml.aspx.cs代码如下:
using System;   
 
using System.Data;  
 
using System.Configuration;  
 
using System.Web;  
 
using System.Web.Security;  
 
using System.Web.UI;  
 
using System.Web.UI.WebControls;  
 
using System.Web.UI.WebControls.WebParts;  
 
using System.Web.UI.HtmlControls;  
 
using System.Data.SqlClient;  
 
using System.Xml;  
 
 
 
public partial class _Default : System.Web.UI.Page   
 
{  
 
    protected void Page_Load(object sender, EventArgs e)  
 
    {  
 
        if(!Page.IsPostBack)  
 
        {   ///从数据库读取XML数据  
 
            ReaderXmlData();  
 
        }  
 
    }  
 
 
 
    private void ReaderXmlData()  
 
    {  
 
        ///创建链接  
 
        SqlConnection myConnection = new SqlConnection(  
 
            ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);  
 
        ///定义SQL语句  
 
        string cmdText = "SELECT * FROM Users FOR XML AUTO";  
 
        ///创建Command  
 
        SqlCommand myCommand = new SqlCommand(cmdText,myConnection);  
 
        try 
 
        {  
 
            ///打开连接  
 
            myConnection.Open();  
 
 
 
            XmlReader myxmlReader = myCommand.ExecuteXmlReader();  
 
 
 
            ///移动到XML元素处  
 
            myxmlReader.MoveToElement();  
 
            ///输出XML文件的标志  
 
            Response.Write("");  
 
            ///输出父节点  
 
            Response.Write("");  
 
            ///读取从数据库中获取的数据  
 
            while(myxmlReader.IsStartElement())  
 
            {  
 
                ///显示从数据库中获取的数据  
 
                Response.Write(myxmlReader.ReadOuterXml());  
 
            }  
 
            Response.Write("
");  
 
            ///关闭XMLReader  
 
            myxmlReader.Close();  
 
        }  
 
        catch(SqlException sqlex)  
 
        {  
 
            ///显示链接错误的消息  
 
            Response.Write(sqlex.Message + "
");  
 
        }  
 
        finally 
 
        {  
 
            ///关闭数据库的链接  
 
            myConnection.Close();  
 
        }  
 
        Response.End();  
 
    }  
 
}  
 
 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="ReaderXml.aspx.cs" Inherits="_Default" %>

 

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

http://www.w3.org/1999/xhtml" >

    从数据库中读取XML数据

   

   

   

   

   


ReaderXml.aspx.cs代码如下:view plaincopy to clipboardprint?using System;     using System.Data;     using System.Configuration;     using System.Web;     using System.Web.Security;     using System.Web.UI;     using System.Web.UI.WebControls;     using System.Web.UI.WebControls.WebParts;     using System.Web.UI.HtmlControls;     using System.Data.SqlClient;     using System.Xml;         public partial class _Default : System.Web.UI.Page      {         protected void Page_Load(object sender, EventArgs e)         {             if(!Page.IsPostBack)             {   ///从数据库读取XML数据                 ReaderXmlData();             }         }             private void ReaderXmlData()         {             ///创建链接             SqlConnection myConnection = new SqlConnection(                 ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);             ///定义SQL语句             string cmdText = "SELECT * FROM Users FOR XML AUTO";             ///创建Command             SqlCommand myCommand = new SqlCommand(cmdText,myConnection);             try            {                 ///打开连接                 myConnection.Open();                     XmlReader myxmlReader = myCommand.ExecuteXmlReader();                     ///移动到XML元素处                 myxmlReader.MoveToElement();                 ///输出XML文件的标志                 Response.Write("");                 ///输出父节点                 Response.Write("");                 ///读取从数据库中获取的数据                 while(myxmlReader.IsStartElement())                 {                     ///显示从数据库中获取的数据                     Response.Write(myxmlReader.ReadOuterXml());                 }                 Response.Write("");                 ///关闭XMLReader                 myxmlReader.Close();             }             catch(SqlException sqlex)             {                 ///显示链接错误的消息                 Response.Write(sqlex.Message + "
");             }             finally            {                 ///关闭数据库的链接                 myConnection.Close();             }             Response.End();         }     }  using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Xml;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

  if(!Page.IsPostBack)

  {   ///从数据库读取XML数据

   ReaderXmlData();

  }

    }

 

 private void ReaderXmlData()

 {

  ///创建链接

  SqlConnection myConnection = new SqlConnection(

   ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);

  ///定义SQL语句

  string cmdText = "SELECT * FROM Users FOR XML AUTO";

  ///创建Command

  SqlCommand myCommand = new SqlCommand(cmdText,myConnection);

  try

  {

   ///打开连接

   myConnection.Open();

 

   XmlReader myxmlReader = myCommand.ExecuteXmlReader();

 

   ///移动到XML元素处

   myxmlReader.MoveToElement();

   ///输出XML文件的标志

   Response.Write("");

   ///输出父节点

   Response.Write("");

   ///读取从数据库中获取的数据

   while(myxmlReader.IsStartElement())

   {

    ///显示从数据库中获取的数据

    Response.Write(myxmlReader.ReadOuterXml());

   }

   Response.Write("");

   ///关闭XMLReader

   myxmlReader.Close();

  }

  catch(SqlException sqlex)

  {

   ///显示链接错误的消息

   Response.Write(sqlex.Message + "
");

  }

  finally

  {

   ///关闭数据库的链接

   myConnection.Close();

  }

  Response.End();

 }

}

本文作者:无为 来源:http://blog.csdn.net/codeshark/archive/2008/07/09/
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读