2012-07-25 80 views
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(); 
     } 

這讓我傳遞值進行的跨向其中輸入manually.However我希望能夠到數據庫從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

你有什麼已經嘗試過? Linq2Xml?用XmlDocument解析?哪些問題你在閱讀文件遇到過嗎? – 2012-07-25 13:35:11

回答

0

我不明白爲什麼我了下來投票,因爲這是一個真正的問題,但是我想我應該說明我如何設法來過這個問題。正如我paticular 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(); 
     } 
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類型的)