user3249735
user3249735

Reputation: 1

Read from txt file and write it to database

Here is the code ...its all ok with code but it just make txt file only whereas I want to make it as it reads the txt file and save it in database......help me please

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)
        { }
    }

Upvotes: 0

Views: 141

Answers (1)

Luaan
Luaan

Reputation: 63722

You never execute the insert command, you have to do cmd.ExecuteNonQuery(); or something like that.

Also, instead of passing the arguments to the command, you're causing a syntax error. Use this instead:

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();

As another sidenote, don't catch all exceptions without doing anything with the caught exception. Have a look at the exceptions thrown, log them, otherwise you have no idea what's going wrong.

Upvotes: 6

Related Questions