我编写了一个从目录读取 XML 文件的服务,它从数据中创建对象并将数据传递到数据库中。我以前有这个工作,但由于某种原因,该服务现在正在创建一个表行,但是它会自动将 0 插入数据库而不是 XML 文件中的数据。我的代码如下:
此 Web 服务方法触发XMLloader
该类:
[WebMethod]
public void RunService()
{
//Call XML loader to get collection of XML documents
foreach (GamePlay value in XMLLoader.Start())
{
value.addNewGamePlay();
}
//call inject data method for each collection
}
此类XMLLoader
返回对象数组GamePlay
:
public static Array Start()
{
string[] filePaths = Directory.GetFiles(@"C:\Users\bradleya\Desktop\XML\");
List<GamePlay> gameObj = new List<GamePlay>();
foreach (string value in filePaths)
{
XmlDocument xd = new XmlDocument();
xd.Load(value);
XmlNode documentNode = xd.SelectSingleNode("/GamePlayData/GamePlay");
GamePlay newGame = new GamePlay();
newGame.setType(Convert.ToString(documentNode.SelectSingleNode("type").InnerText));
newGame.setGamePlayID(Convert.ToInt32(documentNode.SelectSingleNode("GamePlayID").InnerText));
newGame.setParticipantID(Convert.ToInt32(documentNode.SelectSingleNode("ParticipantID").InnerText));
newGame.setGameVersionID(Convert.ToInt32(documentNode.SelectSingleNode("GameVersionID").InnerText));
newGame.setGameID(Convert.ToInt32(documentNode.SelectSingleNode("GameID").InnerText));
newGame.setGameScenarioID(Convert.ToInt32(documentNode.SelectSingleNode("GameScenarioID").InnerText));
newGame.setStartDateTime(Convert.ToDateTime(documentNode.SelectSingleNode("Start").InnerText));
newGame.setEndDateTime(Convert.ToDateTime(documentNode.SelectSingleNode("End").InnerText));
newGame.setSuccess(Convert.ToBoolean(documentNode.SelectSingleNode("Success").InnerText));
gameObj.Add(new GamePlay(gameObj));
}
return gameObj.ToArray();
}
此方法在将对象变量传递给数据访问层的对象类中触发:
public void addNewGamePlay()
{
// dataaccess method call to add GamePlay
// (from the DB) is then set for the object
DataAccessConn.createNewGamePlay(this.ParticipantID, this.GameVersionID, this.GameID, this.GameScenarioID, this.StartDateTime, this.EndDateTime, this.success);
}
最后通过这里的数据访问层方法将数据传入数据库:
public static void createNewGamePlay(int ParticipantID, int GameVersionID,int GameID,int GameSenarioID,DateTime Start,DateTime End,Boolean success)
{
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 = DateTime.Now;
cmd.Parameters.Add(new SqlParameter("@End", SqlDbType.DateTime));
cmd.Parameters["@End"].Value = DateTime.Now;
cmd.Parameters.Add(new SqlParameter("@success", SqlDbType.Bit));
cmd.Parameters["@success"].Value = success;
cmd.ExecuteNonQuery();
}
我试图传递的 XML 文件是:
<?xml version="1.0" encoding="utf-8" ?>
<GamePlayData>
<GamePlay>
<type>add</type>
<GamePlayID>1</GamePlayID>
<ParticipantID>1</ParticipantID>
<GameVersionID>1</GameVersionID>
<GameID>1</GameID>
<GameScenarioID>1</GameScenarioID>
<Start>22/01/2012 23:59:59</Start>
<End>22/01/2012 23:59:59</End>
<Success>False</Success>
</GamePlay>
</GamePlayData>
澄清一下 - 我的问题是我的服务正在创建一个表格行,但是变量自动设置为 0。有人知道为什么这会来自代码吗?
提前致谢。
编辑——按照要求添加的存储过程,谢谢
CREATE PROCEDURE [dbo].[addGamePlay]
@ParticipantID int,
@GameVersionID int,
@GameID int,
@GameScenarioID int,
@Start dateTime,
@End dateTime,
@Success varchar(10)
AS
INSERT INTO GamePlay(ParticipantID,GameVersionID,GameID,GameScenarioID,StartDateTime,EndDateTime,Success)
VALUES (@ParticipantID,@GameVersionID,@GameID,@GameScenarioID,@Start,@End,@Success)
return @@identity