2014-01-29 126 views
0

這裏是代碼...它的代碼一切正常,但它只是使txt文件,而我想讓它讀取txt文件並將其保存在數據庫.. ....幫我請讀取txt文件並將其寫入數據庫

static void Main(string[] args) 
{ 
     McdonaldScrapper(); 
     //PizzaHutScrapper(); 
     //KFCScrapper(); 
} 

private static void McdonaldScrapper() 
{ 
     try 
     { 
      MatchCollection mcl; 
      WebClient webClient = new WebClient(); 
      string strUrl = "http://www.mcdonalds.com.pk/products/view/menu-pricelist"; 
      byte[] reqHTML; 
      reqHTML = webClient.DownloadData(strUrl); 
      UTF8Encoding objUTF8 = new UTF8Encoding(); 
      string pageContent = objUTF8.GetString(reqHTML); 
      int index = pageContent.IndexOf("<div id=\"BodyText\">"); 
      pageContent = pageContent.Substring(index); 


      Regex r = new Regex(@"(<td .*?</td>)"); 
      mcl = r.Matches(pageContent); 
      int count = 0; 
      StringBuilder strBuilder = new StringBuilder(); 
      string name = ""; 
      string price = ""; 
      foreach (Match ml in mcl) 
      { 


       string updatedString = ml.Value.Remove(ml.Value.IndexOf("</td>")); 
       if (count % 2 == 0) 
       { 
        name = updatedString.Remove(0, updatedString.IndexOf('>') + 1); 
       } 
       else 
       { 
        price = updatedString.Remove(0, updatedString.IndexOf('>') + 1); 
        strBuilder.Append(name + " ,  " + price + "\r\n"); 
       } 
       count++; 

      } 

      File.WriteAllText(@"E:\McdonaldsMenu.txt", strBuilder.ToString().Replace("<br />", "").Replace("&amp;", "").Replace("&nbsp;", "")); 
      SaveMcdonaldsMenuToDB(); 
      Console.WriteLine("Press any key to continue"); 
      Console.ReadKey(); 

     } 
     catch (Exception ex) 
     { } 
    } 

    private static void SaveMcdonaldsMenuToDB() 
    { 
     try 
     { 
      int counter = 0; 
      string line; 

      System.IO.StreamReader file = new System.IO.StreamReader("E:\\McdonaldsMenu.txt"); 
      Dictionary<string, string> menuAndPriceList = new Dictionary<string, string>(); 
      while ((line = file.ReadLine()) != null) 
      { 
       if (!menuAndPriceList.ContainsKey(line.Split(',')[0].Trim())) 
       { 
        menuAndPriceList.Add(line.Split(',')[0].Trim(), line.Split(',')[1].Trim()); 

        counter++; 
       } 
      } 

      file.Close(); 
      SqlConnection myConnection = new SqlConnection(); 
      string Constr = @"Data Source=Samsung;Initial Catalog=MAAK FYP; Integrated Security=True"; 
      myConnection = new SqlConnection(Constr); 
      myConnection.Open(); 
//    SqlCommand cmd = new SqlCommand (""); 
//    SqlCommand cmd = new SqlCommand("INSERT into Table33(Product_Name,Product_Price) values(menuAndPriceList[i].key,menuAndPriceList[i].Value)"); 

      for(int i=0; i<menuAndPriceList.Count; i++) 
          { 
     SqlCommand cmd = new SqlCommand("INSERT into Table33(Product_Name,Product_Price) values(menuAndPriceList[i].key,menuAndPriceList[i].Value)"); 

         } 

      Console.ReadLine(); 
     } 
     catch (Exception ex) 
     { } 
    } 
+0

你應該嘗試使用[HTML Agility Pack](http://htmlagilitypack.codeplex.com/)來解析你的html [而不是正則表達式](http://www.codinghorror.com/blog/2009/11/解析HTML的最邪神 - way.html)。 – paqogomez

+0

歡迎來到SO!當您嘗試將數據寫入數據庫時​​,有什麼特別不起作用? – Derek

回答

6

你永遠不會執行插入命令,你必須做cmd.ExecuteNonQuery();或類似的東西。

另外,不是將參數傳遞給命令,而是導致語法錯誤。改爲:

SqlCommand cmd = new SqlCommand("INSERT into Table33(Product_Name,Product_Price) values(@name, @price)"); 
cmd.Parameters.AddWithValue("@name", menuAndPriceList[i].key); 
cmd.Parameters.AddWithValue("@price", menuAndPriceList[i].Value); 
cmd.ExecuteNonQuery(); 

作爲另一個旁註,不要捕獲所有異常,不要對捕獲的異常做任何事情。看看拋出的異常,記錄它們,否則你不知道發生了什麼問題。

相關問題