当Store Procedure数据是xml时,如何使用datareader返回xmlData?

时间:2017-11-20 08:37:09

标签: c# sql-server xml linux stored-procedures

我有一个小的MS SQL表。我还创建了一个只使用简单“选择”案例的基本商店程序。存储过程按我的预期返回xml数据。

**-- This is my MS SQL Store Procedure**
USE [PRIMEWORLD]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[getInstitutionCodes] 

@Data XML  OUTPUT 
AS 
  BEGIN 
    DECLARE  @xml XML 
     
    SET @xml = (SELECT *
                FROM   tbl_Institution_Codes
                for XML RAW) 
     
    SET @Data = @xml 
  END

我的商店处理结果是:

<tbl_Institution_Codes CorporateCode="BELS095" CorporateName="CityHall Water City1" CorporateCategory="CityHall" />
<tbl_Institution_Codes CorporateCode="BELS200" CorporateName="CityHall Water City2" CorporateCategory="CityHall" />
<tbl_Institution_Codes CorporateCode="BELS777" CorporateName="CityHall Water City3" CorporateCategory="CityHall" />
<tbl_Institution_Codes CorporateCode="BELT012" CorporateName="CityHall Traffic City3" CorporateCategory="CityHall" />
<tbl_Institution_Codes CorporateCode="SAT" CorporateName="SatPan1" CorporateCategory="TV/Radio" />
<tbl_Institution_Codes CorporateCode="NYU" CorporateName="New York University" CorporateCategory="Universities" />
<tbl_Institution_Codes CorporateCode="TAx" CorporateName="TaxOffice" CorporateCategory="Goverment" />
<tbl_Institution_Codes CorporateCode="GSM" CorporateName="LmTell" CorporateCategory="Gsm" />

现在我需要将这些数据存入我的嵌入式linux终端。我用c sharp web api。我的问题是如何使用web api将来自存储过程的xml数据返回到我的嵌入式linux终端?

-- This is my web api function

private string GetInstitutionCodesstring id)
        {
            try
            {
                string mFunctionId = id;
                string connectionString = "Data Source=.;Initial Catalog=PRIMEWORLD;User ID=sa;Password=*****";

                SqlConnection cnn;
                cnn = new SqlConnection(connectionString);

                SqlCommand cmd = new SqlCommand();
                DataTable dt = new DataTable();

                cmd.CommandText = "getInstitutionCodesstring";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = cnn;
                cnn.Open();

                string rInstitutionValues = null;

                using (XmlReader reader = cmd.ExecuteXmlReader())
                {
                    while (reader.Read())
                    {
                        // **Question:** How to add the xml values into string
                         rInstitutionValues = reader.ReadOuterXml();
                    }
                }
                // **Question:** How to return xml values
                return rInstitutionValues;
            }
            catch (SqlException ex)
            {
                string msg = "";
                msg = ex.StackTrace;
                return "";
            }
        }

上面有两个问题是尖锐的代码。我帮助将xml数据返回到嵌入式linux终端。 简单地说,我需要在我的web api 中使用DataReader返回My Store Procedure结果到嵌入式linux终端?任何人

由于

更新:工作部分。

while (reader.Read())
{
   string myString = reader.GetString(0);
   returnXmlData = returnXmlData + myString;
};

XmlDocument doc = new XmlDocument();
doc.LoadXml(returnXmlData);
reader.Close();
cnn.Close();

return returnXmlData;

我使用XmlDocument将web api的xml数据返回到嵌入式linux终端

0 个答案:

没有答案