检查数据库是否已更新,然后删除文件

时间:2012-08-06 13:57:40

标签: c# asp.net sql xml

我编写了一个服务,它从目录中读取XML文件,从数据中创建对象并将数据传递到数据库中。我想检查数据库是否已使用每个游戏对象进行更新,如果更新正确,如果由于任何原因未正确添加该文件,我想将文件移动到错误文件夹以供以后处理。

[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); 
   } 

   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> 

addGamePlay存储过程是:

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 

为了澄清我的问题,我想添加一种验证形式,以检查文件是否已正确读取并且数据是否已正确传递到数据库,然后才从目录中删除文件或将其移动到错误文件夹。我正在考虑在对象类中创建一个名为“rootFile”的变量,这样我就可以轻松地删除它并在运行服务方法中添加一个测试,如果它通过测试删除,如果删除,但我不确定语法或是否这个是最好的方式。任何建议表示赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

我的一个简单建议算不上。记录在xml文件中,当您在表中插入记录时,它也返回no。受影响的行数。如果两个数字相等,那么数据库会正确更新,否则会发生一些错误,希望您理解它。