Getting data from a database and returning it in XML

萝らか妹 提交于 2020-02-25 10:07:09

问题


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. I can get all data from a table using this store procedure and it returns in 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:

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(); 
        }

回答1:


To get XML from SQL Server, your stored procedure needs to read something like

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

which will generate an XML result which you can then read with

 var xmlResult = dr[0];

EDIT on clarifications

Replace the datareader in the last line of your second procedure with code similar to your first

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



回答2:


Use cmd.ExecuteXmlReader() to get XmlReader, then use its methods (for example, ReadOuterXml) to get xml.




回答3:


To return a custom xml from stored procedure use the Path method. Below is an example from ADventure works database:

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.



来源:https://stackoverflow.com/questions/11647829/getting-data-from-a-database-and-returning-it-in-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!