我目前在标准网络服务中使用此方法:
[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>
答案 0 :(得分: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)
答案 1 :(得分: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();
}