C#-for循环中的值被覆盖
本文关键字:覆盖 循环 C#-for | 更新日期: 2023-09-27 18:20:55
我正在尝试将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
Post表(帖子将存储在此处)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
回复表(谁对帖子发表评论)//这里的值在我的代码中被覆盖回复_Id客户_Id帖子_Id回复回复日期社区
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();
}
}
}
}
我正在收集列表<>中的数据对于Customer、Response和Post值,然后在CommentM1()方法中循环这些值。
我想得到回应(评论的帖子)正确。
如果有人对这篇"我们提供贷款"的帖子发表评论,post_id和(PostTable)中的customer_id应该存储在响应表中。
有什么想法吗?提前谢谢。
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循环有问题。如果您看到这个循环,它只有在针对每个客户添加了每个注释时才结束。理想情况下,您应该考虑上一个for循环的索引值,并将其从注释列表中过滤出来,然后将其插入dB中。此for循环将始终添加重复的记录。请删除此for,并根据以前的for循环索引值筛选注释。希望这对有帮助