简体   繁体   English

从数据库获取数据并以XML格式返回

[英]Getting data from a database and returning it in XML

I'm currently using a store procedure with a variable to get data from a database but I was the return the result in XML. 我目前正在使用带有变量的存储过程来从数据库中获取数据,但我是以XML格式返回结果。 I can get all data from a table using this store procedure and it returns in XML: 我可以使用此存储过程从表中获取所有数据,并以XML格式返回:

public string GetAllPatients()
        {
            string conn = @"Data Source=SNICKERS\SQLEXPRESS;Initial Catalog=VerveDatabase;Integrated Security=True";
            DataSet oDS = new DataSet();
            SqlDataAdapter oCMD = new SqlDataAdapter("getAll", conn);
            oCMD.Fill(oDS, "AllPatients");
            return oDS.GetXml();
        }

However when I try to get an idividual patient record and return it in XML I'm not sure how, I;m currently doing this: 然而,当我试图得到一个单独的患者记录并以XML格式返回时,我不确定如何,我现在这样做:

public void getUser(int ParticipantID)
        {

            SqlConnection oConn = new SqlConnection();
            oConn.ConnectionString = @"Data Source=SNICKERS\SQLEXPRESS;Initial Catalog=VerveDatabase;Integrated Security=True";
            oConn.Open();
            DataSet oDS = new DataSet();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = oConn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "getUser";
            cmd.Parameters.Add(new SqlParameter("@ParticipantID",SqlDbType.Int));
            cmd.Parameters["@ParticipantID"].Value = 1; 
            SqlDataReader dr = cmd.ExecuteReader(); 
        }

To get XML from SQL Server, your stored procedure needs to read something like 要从SQL Server获取XML,您的存储过程需要读取类似的内容

 SELECT whatever
 FROM thetable
 WHERE ID = @participantID
 FOR XML AUTO

which will generate an XML result which you can then read with 这将生成一个XML结果,然后您可以阅读

 var xmlResult = dr[0];

EDIT on clarifications 编辑澄清

Replace the datareader in the last line of your second procedure with code similar to your first 使用与第一个过程类似的代码替换第二个过程的最后一行中的datareader

        SqlDataAdapter oCMD = new SqlDataAdapter(cmd); 
        oCMD.Fill(oDS, "User"); 
        return oDS.GetXml(); 

使用cmd.ExecuteXmlReader()获取XmlReader ,然后使用其方法(例如, ReadOuterXml )获取xml。

To return a custom xml from stored procedure use the Path method. 要从存储过程返回自定义xml,请使用Path方法。 Below is an example from ADventure works database: 以下是ADventure工作数据库的示例:

select c.customerID as "@ID",
c.accountnumber as "@AccountNumber",
c.rowguid as "comment()",
CAST('<Test/>' as XML ) as "node()",
c.CustomerType as "AdditionalInfo/@type",
c.modifieddate as "AdditionalInfo/text()",
c.rowguid as "node()"
from Sales.Customer c
where c.CustomerID in ( 1,2)
for xml path('Customer'),root('Customers');

 output

<Customers>
<Customer ID="1" AccountNumber="AW00000001">
  <!--3F5AE95E-B87D-4AED-95B4-C3797AFCB74F-->
  <Test />
  <AdditionalInfo type="S">2004-10-13T11:15:07.263</AdditionalInfo>3F5AE95E-B87D-4AED-95B4-C3797AFCB74F</Customer>
<Customer ID="2" AccountNumber="AW00000002">
  <!--E552F657-A9AF-4A7D-A645-C429D6E02491-->
  <Test />
  <AdditionalInfo type="S">2004-10-13T11:15:07.263</AdditionalInfo>E552F657-A9AF-4A7D-A645-C429D6E02491</Customer>
 </Customers>

To return a simple xml see the answer of podiluska. 要返回一个简单的xml,请参阅podiluska的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM