0

我目前在标准 Web 服务中有这种方法:

[WebMethod]
        public void addGame(int GamePlayID, int @ParticipantID, int @GameVersionID, string Start, string End,string success)
        {
            SqlConnection oConn = new SqlConnection();
            oConn.ConnectionString = @"Data Source=SNICKERS\SQLEXPRESS;Initial Catalog=VerveDatabase;Integrated Security=True";
            oConn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = oConn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "addGamePlay";
            cmd.Parameters.Add(new SqlParameter("@GamePlayID", SqlDbType.Int));
            cmd.Parameters["@GamePlayID"].Value = GamePlayID;
            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("@Start", SqlDbType.Time));
            cmd.Parameters["@Start"].Value = Start;
            cmd.Parameters.Add(new SqlParameter("@End", SqlDbType.Time));
            cmd.Parameters["@End"].Value = End;
            cmd.Parameters.Add(new SqlParameter("@success", SqlDbType.VarChar, 10));
            cmd.Parameters["@success"].Value = success;
            cmd.ExecuteNonQuery();
        }

这允许我将值传递给手动输入的数据库。但是我希望能够从 XML 文档加载数据。如何从此 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>
<Game> 
<GamePlayID>1</GamePlayID>
            <ParticipantID>1</ParticipantID> 
<GameVersionID>1</GameVersionID> 
<Start-Time>PT0S</Start-Time> 
<End-Time>PT5H</End-Time> 
<Success>true </Success> 
</Game>
</NewDataSet>
</anyType>
4

2 回答 2

1

您是否考虑过创建一个具有一个 XML 类型参数并发送整个参数的存储过程<NewDataSet>?您可以使用 XQuery 直接从 XML 类型插入到您的表中,类似于INSERT我的应用程序之一:

INSERT INTO CategoryItems (CategoryID, ItemID, SortOrderID) 
    SELECT @ID, 
        ParamValues.Item.value('./ID[1]', 'int'), 
        ParamValues.Item.value('./SortID[1]', 'int')
    FROM @Items.nodes('/Items/Item') AS ParamValues(Item) 

@Items属于 XML 类型)

于 2012-07-25T13:37:27.507 回答
0

我不太明白为什么我被否决了,因为这是一个真正的问题,但我想我会概述我如何设法克服这个问题。由于我的特定 XML 文档只包含一个节点,我使用 xmlDocument 上的“selectSingleNode”函数来选择我想要的数据并将它们分配给准备好通过 SQL proc 插入到我的数据库中的变量。

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

            int GamePlayID = Convert.ToInt32(documentNode.SelectSingleNode("GamePlayID").InnerText);
            int ParticipantID = Convert.ToInt32(documentNode.SelectSingleNode("ParticipantID").InnerText);
            int GameVersionID = Convert.ToInt32(documentNode.SelectSingleNode("ParticipantID").InnerText);
            string Start = Convert.ToString(documentNode.SelectSingleNode("Start-Time").InnerText);
            string End = Convert.ToString(documentNode.SelectSingleNode("End-Time").InnerText);
            string success = Convert.ToString(documentNode.SelectSingleNode("Success").InnerText);

            SqlConnection oConn = new SqlConnection();
            oConn.ConnectionString = @"Data Source=SNICKERS\SQLEXPRESS;Initial Catalog=VerveDatabase;Integrated Security=True";
            oConn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = oConn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "addGamePlay";
            cmd.Parameters.Add(new SqlParameter("@GamePlayID", SqlDbType.Int));
            cmd.Parameters["@GamePlayID"].Value = GamePlayID;
            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("@Start", SqlDbType.Time));
            cmd.Parameters["@Start"].Value = Start;
            cmd.Parameters.Add(new SqlParameter("@End", SqlDbType.Time));
            cmd.Parameters["@End"].Value = End;
            cmd.Parameters.Add(new SqlParameter("@success", SqlDbType.VarChar, 10));
            cmd.Parameters["@success"].Value = success;
            cmd.ExecuteNonQuery();
        }
于 2012-07-25T15:03:31.123 回答