public class ArticlePage
{
public string title;
public string url;
public string rawtext;
public ArticlePage()
{
title = string.Empty;
url = string.Empty;
rawtext = string.Empty;
}
}
public bool mysignal1;//btnworkflow按鈕是否被點擊
public bool mysignal2;
public bool mysignal3;
public bool loading;//工做流按鈕與webbrowser進行交互的通訊按鈕
public bool subloading;
public bool subloadingPer;
public Form1()
{
InitializeComponent();
mysignal1 = false;
mysignal2 = false;
mysignal3 = false;
loading = true;
subloading = true;
subloadingPer = true;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState ==WebBrowserReadyState.Complete)
{
if (mysignal1)
{
if (!mysignal2&&!mysignal3)
{
loading = false;
}
else
{
if (mysignal2&&!mysignal3)
{
subloading = false;
}
if (mysignal3)
{
subloadingPer = false;
}
}
}
}
}
private void btnworkflow_Click(object sender, EventArgs e)
{
mysignal1 = true;
string dirCurPageUrl = string.Empty;//用於恢復目錄頁當前頁的網頁視圖
List<ArticlePage> arListCurrentPage;
foreach (string s in issuesMap)
{
loading = true;
string tmpurl = s;
webBrowser1.Navigate(tmpurl);
while (loading == true)
{
Application.DoEvents();
}
arListCurrentPage = GetArticlePageInfoFromCurrentDirpage();
if (arListCurrentPage != null)
{
dirCurPageUrl = webBrowser1.Url.ToString();
mysignal3 = true;
foreach ( ArticlePage ap in arListCurrentPage)
{
webBrowser1.Navigate(ap.url);
subloadingPer = true;
while(subloadingPer)
{
Application.DoEvents();
}
ap.rawtext = webBrowser1.DocumentText;
}
InsertTitleUrlToDataBase(arListCurrentPage);
mysignal3 = false;
webBrowser1.Navigate(dirCurPageUrl);
loading = true;
while (loading == true)
{
Application.DoEvents();
}
}
mysignal2 = true;
while (AnchorNextPage())
{
subloading = true;
while(subloading)
{
Application.DoEvents();
}
arListCurrentPage = GetArticlePageInfoFromCurrentDirpage();
if (arListCurrentPage != null)
{
dirCurPageUrl = webBrowser1.Url.ToString();
mysignal3 = true;
foreach (ArticlePage ap in arListCurrentPage)
{
webBrowser1.Navigate(ap.url);
subloadingPer = true;
while (subloadingPer)
{
Application.DoEvents();
}
ap.rawtext = webBrowser1.DocumentText;
}
InsertTitleUrlToDataBase(arListCurrentPage);
mysignal3 = false;
webBrowser1.Navigate(dirCurPageUrl);
subloading = true;
while (subloading)
{
Application.DoEvents();
}
}
}
mysignal2 = false;
//得到當前頁面的下一頁連接
}
}
private void InsertTitleUrlToDataBase(List<ArticlePage> arlist)
{
DataBaseManipulation dm = new DataBaseManipulation();
string conStr = "server=(local);database=xxxxx;uid=sa;pwd=xxx";
dm.ConstructConnection(conStr);
foreach (ArticlePage article in arlist)
{
dm.InsertToDataBase(article, "xxx");
}
}
public void InsertToDataBase(ArticlePage article,string table )
{ //插入字符串
string sqlcommand=string.Format("insert into {0}(ArticlePageUrl,ArticlePageTitle,ArticlePageSource)values(@ArticlePageUrl,@ArticlePageTitle,@ArticlePageSource)",table);
//數據庫參數構造與賦初值
SqlParameter ArticlePageTitle = new SqlParameter("@ArticlePageTitle", SqlDbType.VarChar, 400);
ArticlePageTitle.Value = article.title;
SqlParameter ArticlePageUrl = new SqlParameter("@ArticlePageUrl", SqlDbType.VarChar, 400);
ArticlePageUrl.Value = article.url;
SqlParameter ArticlePageSource = new SqlParameter("@ArticlePageSource", SqlDbType.Text);
ArticlePageSource.Value = article.rawtext;
SqlCommand cmd = new SqlCommand(sqlcommand, connection);
cmd.Parameters.Add(ArticlePageTitle);
cmd.Parameters.Add(ArticlePageUrl);
cmd.Parameters.Add(ArticlePageSource);
//打開數據庫鏈接
OpenConnection();
try
{
//執行cmd操做
cmd.ExecuteNonQuery();
}
catch (System.Exception e)
{ //輸出錯誤到記事本中
StreamWriter sw = new StreamWriter("D:\\myerror.txt", true, Encoding.Default);
sw.Write(e.Message);
sw.Close();
//一旦發生錯誤程序就中止運行,等待用戶發現
Console.Read();
}
//關閉數據庫鏈接
CloseConnection();
}
}
}