LinkedIn からデータを取得してデータベースに保存しようとしています。
ループに問題があります
1.最初の foreach では、データを挿入しています。
2. (user_id) を 2 番目の挿入ステートメントに渡します。
3. 2 番目の foreach でデータを挿入したら、最初に foreach を実行します。
私はこのコードを書いた
public void customerM()
{
XmlDocument d = new XmlDocument();
d.LoadXml(content);
XmlNodeList customer = d.SelectNodes("//posts/post/creator");
foreach (XmlNode cus in customer)
{
user_id = cus["id"].InnerText;
customerFirst_name = cus["first-name"].InnerText;
customerLast_name = cus["last-name"].InnerText;
customerName = string.Format("{0} {1}", customerFirst_name, customerLast_name);
DbConnection.Open();
DbCommand = new OleDbCommand("select count(*) from mw_customer where customer_id='" + user_id + "'", DbConnection);
OleDbDataReader DbReader = DbCommand.ExecuteReader();
while (DbReader.Read())
{
count = DbReader[0].ToString();
cnt = Convert.ToInt32(count);
if (cnt == 0)
{
DbCommand = new OleDbCommand("insert into mw_customer(customer_id,name,first_name,last_name,user_id,cust_date,community) values('" + user_id + "','" + customerName + "','" + customerFirst_name + "','" + customerLast_name + "','" + user_id + "','" + post_timestamp + "','LinkedIn')", DbConnection);
DbCommand.ExecuteNonQuery();
}
else
{
DbCommand = new OleDbCommand("UPDATE mw_customer set name='" + customerName + "',first_name='" + customerFirst_name + "',last_name='" + customerLast_name + "',user_id='" + user_id + "',community = 'LinkedIn',cust_date='" + post_timestamp + "' where customer_id = '" + user_id + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
}
DbReader.Close();
XmlNodeList title = d.SelectNodes("//posts/post");
foreach (XmlNode post in title)
{
posts = post["title"].InnerText;
post_id = post["id"].InnerText;
string postTimeStamp = post["creation-timestamp"].InnerText;
double postTimeStamp1 = Convert.ToDouble(postTimeStamp);
DateTime post_timestamp1 = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Math.Round(postTimeStamp1 / 1000d)).ToLocalTime();
post_timestamp = post_timestamp1.ToString("dd-MMM-yy");
DbCommand = new OleDbCommand("select count(*) from mw_post where post_id = '" + post_id + "'", DbConnection);
OleDbDataReader DbReader1 = DbCommand.ExecuteReader();
while (DbReader1.Read())
{
count = DbReader1[0].ToString();
cnt = Convert.ToInt32(count);
if ((cnt == 0) && (posts != ""))
{
DbCommand = new OleDbCommand("insert into mw_post(post_id,customer_id,post,post_date,community) values('" + post_id + "','" + user_id + "','" + posts + "', '" + post_timestamp + "','LinkedIn')", DbConnection);
DbCommand.ExecuteNonQuery();
if (posts.ToUpper().Contains("Personal Loan".ToUpper()))
{
DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '2',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
else if (posts.ToUpper().Contains("Credit Card".ToUpper()))
{
DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '1',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
else if (posts.ToUpper().Contains("Home Loan".ToUpper()))
{
DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '3',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
else if (posts.ToUpper().Contains("Car Loan".ToUpper()))
{
DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '4',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
else if (posts.ToUpper().Contains("Deposit".ToUpper()))
{
DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '5',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
else if (posts.ToUpper().Contains("Debit Card".ToUpper()))
{
DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '7',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
else
{
DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '6',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
DbCommand.ExecuteNonQuery();
}
}
}
DbReader1.Close();
DbConnection.Close();
}
}
}
XML ファイル
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n
<posts total=\"1\">\n <post>\n
<creation-timestamp>
1362659632000
</creation-timestamp>\n
<id>
g-4891367-S-dfdsfd
</id>\n
<title>
dfsdfsdf
</title>\n
<creator>\n
<id>
dfdsfsdf
</id>\n
<first-name>
dfsdfsd
</first-name>\n
<last-name>
dsfsdfd
</last-name>\n
<picture-url>
http://m3.licdn.com/mpr/mprx/0_zR- 8Kkl9rtYlGl1YcyCPK6tMrqDrGlCYBM60KFPQftdfsdfsdOJSfTn_tq4rYx8Svbl9K
</picture-url>\n
<headline>
dfsdfsd
</headline>\n
</creator>\n
<comments total=\"1\">\n
<comment>\n
<creation-timestamp>
1372061224000
</creation-timestamp>\n
<id>
g-4891367-S-dsfsdfsd
</id>\n
<text>
dfsdfsd.
</text>\n
<creator>\n
<id>
C-dfsdfsd
</id>\n
<first-name>
dfsdfsd
</first-name>\n
<last-name>
dfsdfsd
</last-name>\n
<headline>
dsfsdsd
</headline>\n
<picture-url>
http://m3.licdn.com/mpr/mprx/0_OSEuUB4ZdfsdfUzsFvCLad5Y2yfY1Uqo4EXAO93Wutws8zNY9U75AECxh0metNPq6zdHm
</picture-url>\n
</creator>\n
<creation-timestamp>
1372061224895
</creation-timestamp>\n
<relation-to-viewer>\n
<available-actions total=\"1\">\n
<action>\n
<code>delete</code>\n
</action>\n
</available-actions>\n
</relation-to-viewer>\n
</comment>\n
</comments>\n
<likes total=\"2\">\n
<like>\n
<person>\n
<id>
dfsfwe
</id>\n
<first-name>
dfsdfsd
</first-name>\n
<last-name>
dfsd
</last-name>\n
<headline>
dfsdf
</headline>\n
<picture-url>
http://m3.licdn.com/mpr/mprx/0dfsdfsdda7-q79MqA-X55lrEa7- tssMzKfRilTfaJfyrYdcvX3oeLmPSf_AzuRntGlAaCl
</picture-url>\n
</person>\n
</like>\n <like>\n
<person>\n
<id>
S4Er-sdjfjd
</id>\n
<first-name>
fdsfs
</first-name>\n
<last-name>
dfsdd
</last-name>\n
<headline>
sdfsdfsdfds
</headline>\n
<picture-url>
http://m3.licdn.com/mpr/mprx/0_kdfsdfdsfsdo
</picture-url>\n
</person>\n
</like>\n </likes>\n
<relation-to-viewer>\n
<is-following>true</is-following>\n
<is-liked>false</is-liked>\n
<available-actions total=\"6\">\n <action>\n
<code>add-comment</code>\n
</action>\n <action>\n
<code>categorize-as-job</code>\n
</action>\n <action>\n
<code>categorize-as-promotion</code>\n
</action>\n <action>\n
<code>delete</code>\n
</action>\n <action>\n
<code>like</code>\n
</action>\n <action>\n
<code>unfollow</code>\n
</action>\n </available-actions>\n
</relation-to-viewer>\n </post>\n</posts>\n"
何か案は?前もって感謝します。