C# - 在for循环中覆盖了值?

时间:2013-07-04 10:42:42

标签: c# asp.net for-loop oracle-sqldeveloper

我正在尝试将LinkedIn集成到我的应用中,值会在响应表中被覆盖

我有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

我写过这段代码

获取List<>

中的所有评论
  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();
                }
            }
        }
    }

我正在收集List&lt;&gt;中的数据对于Customer,Response和Post值,然后在CommentM1()方法中循环该值。

我希望得到回复(对帖子的评论)。

如果有人对此帖子发表评论“我们正在提供贷款”,Post_id和来自(邮政表)的customer_id应该存储到响应表中。

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分: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循环索引值删除此for和过滤注释。希望这有用