2013-07-04 65 views
0

我想LinkedIn在我的應用程序集成,值將被覆蓋在響應表格C# - for循環中的值被覆蓋?

我有3個表

客戶表(誰在LinkedIn的職位他們的詳細信息將存儲在此表)

Customer_Id Name Cust_date User_id Community 

Efthok  xxxx 04-Mar-13 Efthok LinkedIn 
df343n  yyyy 27-Jun-13 df343n LinkedIn 
4retee  zzzz 01-Jul-13 4retee LinkedIn 

帖子表(帖子將存儲在這裏)CUSTOMER_ID是Customer表

Customer_Id Post_Id Posts     PostDate  Community 

Efthok  guujjk intersted in car loan 04-Mar-2013 LinkedIn 
df343n  fdg4df we are offering loans 27-Jun-2013 LinkedIn 
4retee  hgf454 ******************** 01-Jul-2013 LinkedIn 

響應的外鍵表(誰給的職位評論) //這裏的值在我的代碼重寫 RESPONSE_ID CUSTOMER_ID POST_ID響應ResponseDate社區

767hhjj  Efthok  guujjk let me know the interest 06-Apr-2013 Linked 
gdf5654  Efthok  guujjk let me know the interest 06-Apr-2013 Linked 

我寫這段代碼

爲了得到在列表中的所有評論<>

public void commentM() 
    { 
     XmlDocument d = new XmlDocument(); 
     d.LoadXml(content); 

     XmlNodeList comments = d.SelectNodes("//comments/comment"); 
     foreach (XmlNode xncomment in comments) 
     { 
      commentId = xncomment["id"].InnerText; 
      memComments = xncomment["text"].InnerText; 
      string timeStamp = xncomment["creation-timestamp"].InnerText; 
      double cmtTimeStamp1 = Convert.ToDouble(timeStamp); 
      DateTime comment_timestamp1 = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Math.Round(cmtTimeStamp1/1000d)).ToLocalTime(); 
      comment_timestamp = comment_timestamp1.ToString("dd-MMM-yy"); 

      commentData = new CommentData { CommentId = commentId, Comments = memComments, CommentTimeStamp = comment_timestamp }; 
      listComment.Add(commentData); 
      commentM1(); 
     } 
    } 

public void commentM1() 
    { 
     for (int i = 0; i < listCustomer.Count; i++) 
     { 
      var grt = listCustomer[i]; 
      id = grt.UserId; 

      for (int k = 0; k < listPost.Count; k++) 
      { 
       string post_id1 = listPost[k].PostId; 

       for (int j = 0; j < listComment.Count; j++) 
       { 
        comId = listComment[j].CommentId; 
        comments1 = listComment[j].Comments; 
        commentTime = listComment[j].CommentTimeStamp; 

        DbConnection.Open(); 
        DbCommand = new OleDbCommand("select count(response_id) from mw_response where response_id = '" + comId + "'", DbConnection); 
        OleDbDataReader DbReader = DbCommand.ExecuteReader(); 

        while (DbReader.Read()) 
        { 
         count = DbReader[0].ToString(); 
         cnt = Convert.ToInt32(count); 

         if ((cnt == 0) && (memComments != "")) 
         { 
          DbCommand = new OleDbCommand("insert into mw_response(post_id,response,response_id, resp_date,community) values('" + post_id1 + "','" + comments1 + "','" + comId + "','" + commentTime + "','LinkedIn')", DbConnection); 
          DbCommand.ExecuteNonQuery(); 

          //update productid and customerid 
          DbCommand = new OleDbCommand("update mw_response set prod_id = (select prod_id from mw_post where post_id='" + post_id1 + "'),customer_id = (select customer_id from mw_customer where customer_id = '" + id + "') where response_id = '" + comId + "'", DbConnection); 
          DbCommand.ExecuteNonQuery(); 

                 } 
        } 
        DbReader.Close(); 
        DbConnection.Close(); 
       } 
      } 
     } 
    } 

我收集數據以列表<>爲顧客,反應和後值,則循環在CommentM1值()方法。

我想正確地得到Response(帖子的評論)。

如果有人對此帖子發表評論「我們提供貸款」,Post_id和來自(Post Table)的customer_id應存儲到響應表中。

任何想法?提前致謝。

+0

我懷疑問題是在無論創建listComment',我們不能看到該代碼... –

+0

感謝您的答覆。我只是添加了listComment,請仔細閱讀 – user2500094

+1

您正在'commentM'的循環*中調用'commentM1'(奇怪的名字,btw)* ......這肯定不是您想要做的。 –

回答

0
 for (int k = 0; k < listPost.Count; k++) 
     { 
      string post_id1 = listPost[k].PostId; 

      for (int j = 0; j < listComment.Count; j++) 
      { 
       comId = listComment[j].CommentId; 
       comments1 = listComment[j].Comments; 
       commentTime = listComment[j].CommentTimeStamp; 

       DbConnection.Open(); 
       DbCommand = new OleDbCommand("select count(response_id) from  mw_response where response_id = '" + comId + "'", DbConnection); 
       OleDbDataReader DbReader = DbCommand.ExecuteReader(); 

       while (DbReader.Read()) 
       { 
        count = DbReader[0].ToString(); 
        cnt = Convert.ToInt32(count); 

        if ((cnt == 0) && (memComments != "")) 
        { 
         DbCommand = new OleDbCommand("insert into mw_response(post_id,response,response_id, resp_date,community) values('" + post_id1 + "','" + comments1 + "','" + comId + "','" + commentTime + "','LinkedIn')", DbConnection); 
         DbCommand.ExecuteNonQuery(); 

         //update productid and customerid 
         DbCommand = new OleDbCommand("update mw_response set prod_id = (select prod_id from mw_post where post_id='" + post_id1 + "'),customer_id = (select customer_id from mw_customer where customer_id = '" + id + "') where response_id = '" + comId + "'", DbConnection); 
         DbCommand.ExecuteNonQuery(); 

                } 
       } 
       DbReader.Close(); 
       DbConnection.Close(); 
      } 
     } 

這個特殊的循環有問題。如果你看到這個循環,它只有在每個顧客都添加了每條評論時纔會結束。理想情況下,您應該考慮前一個for循環的索引值,並通過它來過濾來自評論列表的評論並將其插入dB中。這個for循環將總是添加重複記錄。請將其刪除,並根據以前的for循環索引值過濾註釋。希望這是有幫助的

+0

感謝你的努力,我嘗試了你的代碼方式,但仍然被覆蓋 – user2500094