以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML 与 数据库 』  (http://bbs.xml.org.cn/list.asp?boardid=17)
----  [求助]谁会SQL数据库转成XML格式的文档  (http://bbs.xml.org.cn/dispbbs.asp?boardid=17&rootid=&id=18720)


--  作者:playboylinlin
--  发布时间:5/23/2005 4:26:00 PM

--  [求助]谁会SQL数据库转成XML格式的文档
本人现在急需把SQL数据库的东西转成XML格式的文档,这样的一个工具的源代码,那位大哥或大姐,会的话帮小弟编一个 ,语言没有限制,只要有这个功能.先谢谢各位了
本人QQ:9472398
--  作者:canxing
--  发布时间:5/25/2005 9:42:00 AM

--  
我也想要了……
--  作者:keeponline
--  发布时间:6/11/2005 12:51:00 PM

--  
我也想要
--  作者:jx27
--  发布时间:6/17/2005 6:50:00 PM

--  
呵呵,得用PHP。

没编过,不好意思,帮不到你


--  作者:webdev
--  发布时间:6/27/2005 1:08:00 PM

--  
自己写个啊,取得表的字段名和字段值,然后封装就行了,不过外围的标签你还是需要自己定义的。
--  作者:315zxl
--  发布时间:8/5/2005 4:25:00 PM

--  
自己写一个呀!!
--  作者:xcccb
--  发布时间:9/5/2005 6:46:00 PM

--  
用asp.net来写:
sqlcommand.commandText="select * from employee for xml auto,xmldata"
sqlcommand.commandType = CommandType.Text
dim xmlReader as xml.xmlTextReader
xmlReader = sqlcommand.executexmlreader
dataset.readxml(xmlreader,xmlreadmode.fragment)
dim filename as filestream = new filestream("test.xml",filemode.create)
ds.writexml("test.xml")
--  作者:弄清影
--  发布时间:9/23/2005 1:34:00 PM

--  

首先建立与数据库的连接,本例子用SQL Server中的Northwind数据库中的Supplier表,为Supplier表创建存储过程命名为GetSupplierXml
seleclt............
for xml auto

用.net平台C# 语言
首先创建SupplierListDB组件如下:
using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

namespace SupplierListDB
{
  public class SupplierList
  {
//连接数据库
    private String m_ConnectString;
    public SupplierList(String ConnectString)
    {
      m_ConnectString = ConnectString;
    }
    public DataSet GetSuppliersDataSet()
    {

//调用存储过程String strQuery = "GetSupplierList";

      OleDbConnection objConnect = new OleDbConnection(m_ConnectString);
      OleDbDataAdapter objAdapter = new OleDbDataAdapter(strQuery, objConnect);
      DataSet objDataSet = new DataSet();
      try
      {

        // get the data into a table named "Suppliers" in the DataSet
        objAdapter.Fill(objDataSet, "Suppliers");
        // return the DataSet object to the calling routine
        return objDataSet;
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
    }
    public OleDbDataReader GetSuppliersDataReader()
    {

      // declare a String containing the stored procedure name
      String strQuery = "GetSupplierList";
      // create a new Connection object using connection string
      OleDbConnection objConnect = new OleDbConnection(m_ConnectString);
      // create new Command using stored proc name and Connection
      OleDbCommand objCommand = new OleDbCommand(strQuery, objConnect);
      try
      {

        // 打开连接
        objConnect.Open();
        // execute the stored proc to initialize the DataReader
        // connection will be closed when DataReader goes out of scope
        return objCommand.ExecuteReader(CommandBehavior.CloseConnection);
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
    }
      // method to return an XML document (as a String) containing
    // a full list of supplier details from database
    public String GetSuppliersXml(Boolean IncludeSchema)
    {

      // declare a String containing the stored procedure name
      String strQuery = "GetSupplierList";
      // create a new Connection object using connection string
      OleDbConnection objConnect = new OleDbConnection(m_ConnectString);
      // create new DataAdapter using stored proc name and Connection
      OleDbDataAdapter objAdapter = new OleDbDataAdapter(strQuery, objConnect);
      // create a new DataSet object to hold the results
      DataSet objDataSet = new DataSet();
      // declare an empty String to hold the results
      String strXml = String.Empty;
      try
      {
        // get the data into a table named "Suppliers" in the DataSet
        objAdapter.Fill(objDataSet, "Suppliers");
        // get schema if ( specif (ied in optional method parameter
        if (IncludeSchema == true)
          strXml = objDataSet.GetXmlSchema() + (char)13 + (char)10 + (char)13 + (char)10;
        // get XML data and append to String
        strXml += objDataSet.GetXml();
        // return the XML string to the calling routine
        return strXml;
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
    }
    // ----------------------------------------------------

    // method to return an XmlDocument object containing
    // a full list of supplier details from database
    public XmlDocument GetSuppliersXmlDocument()
    {

      // declare a String containing the stored procedure name
      String strQuery = "GetSupplierList";

      // create a new Connection object using connection string
      OleDbConnection objConnect = new OleDbConnection(m_ConnectString);

      // create new DataAdapter using stored proc name and Connection
      OleDbDataAdapter objAdapter = new OleDbDataAdapter(strQuery, objConnect);

      // create a new DataSet object to hold the results
      DataSet objDataSet = new DataSet();

      try
      {

        // get the data into a table named "Suppliers" in the DataSet
        objAdapter.Fill(objDataSet, "Suppliers");

        // create a new XmlDataDocument object based on the DataSet
        XmlDataDocument objXmlDataDoc = new XmlDataDocument(objDataSet);

        // return it an XmlDocument object to the calling routine
        return (XmlDocument)objXmlDataDoc;
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
    }

    // method to return an XML document (as a String) containing
    // a full list of supplier details from database, and using
    // a SQL Server XML technology (FOR XML) query
    public String GetSuppliersSqlXml()
    {

      // declare a String containing the SQL-XML stored proc to execute
      String strQuery = "GetSupplierXml";

      // create a new Connection object using connection string
      SqlConnection objConnect = new SqlConnection(m_ConnectString);

      // create new Command using stored proc name and Connection
      SqlCommand objCommand = new SqlCommand(strQuery, objConnect);

      // create a variable to hold an XmlTextReader object
      XmlTextReader objReader = null;

      String strXml = String.Empty;
      Char QUOT = (char)34;
      try
      {

        // open connection to the database
        objConnect.Open();

        // execute the stored proc to initialize the XmlTextReader
        objReader = (XmlTextReader) objCommand.ExecuteXmlReader();

        // create the document prolog
        DateTime datToday = DateTime.Now;
        strXml = "<?xml version=" + QUOT + "1.0" + QUOT + "?>"
          + "<!-- Created: " + datToday.ToString("d") + " -->"
          + "<SupplierList>";

        // read the first result row and ) read remainder
        objReader.ReadString();
        strXml += objReader.GetRemainder().ReadToEnd();

        // add the document epilog
        strXml += "</SupplierList>";
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
      finally
      {
        // close connection and destroy reader object
        objConnect.Close();
        objReader = null;
      }
      // return the XML document object to the calling routine
      return strXml;
    }
    // ----------------------------------------------------

    // method to return an Array containing the full
    // name and address details of suppliers from the database
    public String[,] GetSuppliersArray(ref int MaximumRowNumber)
    {

      // declare a String containing the stored procedure name
      String strQuery = "GetSupplierList";

      // create a new Connection object using connection string
      OleDbConnection objConnect = new OleDbConnection(m_ConnectString);

      // create new Command using stored proc name and Connection
      OleDbCommand objCommand = new OleDbCommand(strQuery, objConnect);

      // create a variable to hold a DataReader object
      OleDbDataReader objReader = null;

      try
      {

        // open connection to the database
        objConnect.Open();

        // execute query to initialize DataReader object
        // connection will be closed when DataReader goes out of scope
        objReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);

        // get the index of the last column within the data
        int intLastColIndex = objReader.FieldCount;

        // declare a variable to hold the result array
        String[,] arrValues = new String[intLastColIndex, MaximumRowNumber];

        int intRowCount = 0;   // to hold number of rows returned
        int intCol;            // to hold the column iterator

        // iterate through rows by calling Read method until false
        while (objReader.Read() && intRowCount < MaximumRowNumber)
        {

          // store column values strings in result array
          for (intCol = 0; intCol < intLastColIndex; intCol++)
            arrValues[intCol, intRowCount] = (String) objReader[intCol];

          // increment number of rows found
          intRowCount++;

        }

        objReader = null;     // finished with DataReader

        // set Ref parameter value for number of rows in Array
        MaximumRowNumber = intRowCount - 1;

        return arrValues;   // and return array to the calling routine
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
    }
    // ----------------------------------------------------

    // method to return an ArrayList containing just the
    // name of each of the suppliers in the database
    public ArrayList GetSuppliersArrayList()
    {

      // declare a String containing the stored procedure name
      String strQuery = "GetSupplierName";

      // create a new Connection object using connection string
      OleDbConnection objConnect = new OleDbConnection(m_ConnectString);

      // create new Command using stored proc name and Connection
      OleDbCommand objCommand = new OleDbCommand(strQuery, objConnect);

      // create a variable to hold a DataReader object
      OleDbDataReader objReader = null;

      try
      {

        // open connection to the database
        objConnect.Open();

        // execute query to initialize DataReader object
        // connection will be closed when DataReader goes out of scope
        objReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);

        // create a new ArrayList object
        ArrayList arrValues = new ArrayList();

        // iterate through rows by calling Read method until false
        while (objReader.Read())
          arrValues.Add(objReader.GetString(0));

        objReader = null;     // finished with DataReader

        return arrValues;   // return ArrayList to the calling routine
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
    }
    // ----------------------------------------------------

  }
}


--  作者:弄清影
--  发布时间:9/23/2005 1:38:00 PM

--  

不好意思,上次代码有冗余,
用.net平台C# 语言
首先创建SupplierListDB数据访问组件如下:
using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

namespace SupplierListDB
{
  public class SupplierList
  {
//连接数据库
    private String m_ConnectString;
    public SupplierList(String ConnectString)
    {
      m_ConnectString = ConnectString;
    }
    public DataSet GetSuppliersDataSet()
    {

//调用存储过程String strQuery = "GetSupplierList";

      OleDbConnection objConnect = new OleDbConnection(m_ConnectString);
      OleDbDataAdapter objAdapter = new OleDbDataAdapter(strQuery, objConnect);
      DataSet objDataSet = new DataSet();
      try
      {

        // get the data into a table named "Suppliers" in the DataSet
        objAdapter.Fill(objDataSet, "Suppliers");
        // return the DataSet object to the calling routine
        return objDataSet;
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
// method to return an XML document (as a String) containing
    // a full list of supplier details from database, and using
    // a SQL Server XML technology (FOR XML) query
    public String GetSuppliersSqlXml()
    {

      // declare a String containing the SQL-XML stored proc to execute
      String strQuery = "GetSupplierXml";

      // create a new Connection object using connection string
      SqlConnection objConnect = new SqlConnection(m_ConnectString);

      // create new Command using stored proc name and Connection
      SqlCommand objCommand = new SqlCommand(strQuery, objConnect);

      // create a variable to hold an XmlTextReader object
      XmlTextReader objReader = null;

      String strXml = String.Empty;
      Char QUOT = (char)34;
      try
      {

        // open connection to the database
        objConnect.Open();

        // execute the stored proc to initialize the XmlTextReader
        objReader = (XmlTextReader) objCommand.ExecuteXmlReader();

        // create the document prolog
        DateTime datToday = DateTime.Now;
        strXml = "<?xml version=" + QUOT + "1.0" + QUOT + "?>"
          + "<!-- Created: " + datToday.ToString("d") + " -->"
          + "<SupplierList>";

        // read the first result row and ) read remainder
        objReader.ReadString();
        strXml += objReader.GetRemainder().ReadToEnd();

        // add the document epilog
        strXml += "</SupplierList>";
      }
      catch (Exception objErr)
      {
        throw (objErr);
      }
      finally
      {
        // close connection and destroy reader object
        objConnect.Close();
        objReader = null;
      }
      // return the XML document object to the calling routine
      return strXml;
    }
    }
以下是ASP.NET代码
<%@Page Language="C#" %>
<%@Import Namespace="SupplierListDB" %>

<!------------------ HTML page content --------------------->

<html>
<head>
<title>Supplier List : XML Document from Relational Database</title>
<!-- #include file="../global/style.inc" -->
</head>
<body>

<div class="heading">Supplier List : XML Document from Relational Database</div>

<div align="right" class="cite">
[<a href="../global/viewsource.aspx?compsrc=get-supplier-data.cs">view page source</a>]<br />
[<a href="viewxml.aspx?doc=GetSuppliersXml">view XML document</a>]<br />
[<a href="viewxml.aspx?doc=GetSuppliersXmlStyle">view XSLT sylesheet</a>]
</div><br />

<asp:Label id="lblMessage" runat="server" />
<asp:Xml id="xmlResult" runat="server" /><br />

<!-- #include file="../global/foot.inc" -->
</body>
</html>

<!-------------- server-side script section ---------------->

<script Language="C#" runat="server">

void Page_Load() {

  // get connection string from web.config
  String strConnect = ConfigurationSettings.AppSettings["NorthwindConnectString"];

  try {

    // create an instance of the data access component
    SupplierList objSupplierList = new SupplierList(strConnect);

    // call the method to return the data as an Xml String and
    // specify false as no schema required
    // assign it to the Xml Server Control
    xmlResult.DocumentContent = objSupplierList.GetSuppliersXml(false);

    // specify path to XSLT stylesheet that transforms XML for display
    xmlResult.TransformSource = "supplier-list-style.xsl";
  }
  catch (Exception objErr) {

    // there was an error and no data will be returned
    lblMessage.Text = "ERROR: No data returned. " + objErr.Message;
  }
}
</script>


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
12,062.500ms