0

我创建了这个返回表主键的存储过程。我在 Web 服务中调用此存储过程并传递来自 XML 文档的数据。我想在 Web 服务上以 XML 格式输出返回的主键,以确认数据库中的数据已更新。存储过程的代码:

Create PROCEDURE [dbo].[updateGamePlay]
    @GamePlayID int,
    @ParticipantID int,
    @GameVersionID int,
    @GameID int,
    @GameScenarioID int,
    @Start DateTime,
    @End DateTime,
    @Success varchar(10)
    AS
    UPDATE GamePlay
    SET 
    ParticipantID = @ParticipantID,GameVersionID = @GameVersionID,GameID = @GameID,GameScenarioID = @GameScenarioID,StartDateTime = @Start,EndDateTime = @End,Success = @Success
    WHERE GamePlayID = @GamePlayID
    RETURN SCOPE_IDENTITY(); 

Web 服务将 XML 中的数据分配给局部变量并将其插入数据库。我想获取返回类型并将其以 XML 形式显示在 Web 服务上。

[WebMethod]
        public void AddGamePlayData()
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(@"C:\Users\bradleya\Documents\Visual Studio 2010\Projects\Web Services\Web Services\addGame.xml");
            XmlNode documentNode = xd.SelectSingleNode("/GamePlayData/GamePlay");

            int ParticipantID = Convert.ToInt32(documentNode.SelectSingleNode("ParticipantID").InnerText);
            int GameVersionID = Convert.ToInt32(documentNode.SelectSingleNode("GameVersionID").InnerText);
            int GameID = Convert.ToInt32(documentNode.SelectSingleNode("GameID").InnerText);
            int GameSenarioID = Convert.ToInt32(documentNode.SelectSingleNode("GameScenarioID").InnerText);
            DateTime Start = Convert.ToDateTime(documentNode.SelectSingleNode("Start").InnerText);
            DateTime End = Convert.ToDateTime(documentNode.SelectSingleNode("End").InnerText);
            Boolean success = Convert.ToBoolean(documentNode.SelectSingleNode("Success").InnerText);

            SqlConnection oConn = new SqlConnection();
            oConn.ConnectionString = @"Data Source=SNICKERS\SQLEXPRESS;Initial Catalog=VervePhaseOne;Integrated Security=True";
            oConn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = oConn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "addGamePlay";
            cmd.Parameters.Add(new SqlParameter("@ParticipantID", SqlDbType.Int));
            cmd.Parameters["@ParticipantID"].Value = ParticipantID;
            cmd.Parameters.Add(new SqlParameter("@GameVersionID", SqlDbType.Int));
            cmd.Parameters["@GameVersionID"].Value = GameVersionID;
            cmd.Parameters.Add(new SqlParameter("@GameID", SqlDbType.Int));
            cmd.Parameters["@GameID"].Value = GameID;
            cmd.Parameters.Add(new SqlParameter("@GameScenarioID", SqlDbType.Int));
            cmd.Parameters["@GameScenarioID"].Value = GameSenarioID;
            cmd.Parameters.Add(new SqlParameter("@Start", SqlDbType.DateTime));
            cmd.Parameters["@Start"].Value = Start;
            cmd.Parameters.Add(new SqlParameter("@End", SqlDbType.DateTime));
            cmd.Parameters["@End"].Value = End;
            cmd.Parameters.Add(new SqlParameter("@success", SqlDbType.Bit));
            cmd.Parameters["@success"].Value = success;
            cmd.ExecuteNonQuery();

        }

我试图将返回类型更改为对象并使用以下代码,但它不起作用:

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

- -编辑 - -

当我使用我认为应该工作的修复时的错误是 XML 应该包含一个整数,因为这应该返回但是它返回这个 XML:

<?xml version="1.0" encoding="UTF-8"?>
<anyType xmlns="http://tempuri.org/" xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" d1p1:type="q1:string" xmlns:q1="http://www.w3.org/2001/XMLSchema">
<NewDataSet />
</anyType>

提前致谢

4

1 回答 1

0

如果你想要一个返回值,你应该使用一个函数,否则使用一个 select 语句来输出你的结果。

像这样的东西:

Create PROCEDURE [dbo].[updateGamePlay]
    @GamePlayID int,
    @ParticipantID int,
    @GameVersionID int,
    @GameID int,
    @GameScenarioID int,
    @Start DateTime,
    @End DateTime,
    @Success varchar(10)
AS
    UPDATE GamePlay
    OUTPUT INSERTED.GamePlayID
    SET 
    ParticipantID = @ParticipantID,GameVersionID = @GameVersionID,GameID = @GameID,GameScenarioID = @GameScenarioID,StartDateTime = @Start,EndDateTime = @End,Success = @Success
WHERE GamePlayID = @GamePlayID
于 2012-08-02T08:14:29.173 回答