将InnerHtml值转换为Decimal

时间:2016-01-21 11:25:28

标签: c# sql-server html-agility-pack

我试图将InnerHtml值转换为十进制值。用户将XPath输入 textBox2

List<Decimal> productPriceList = new List<Decimal>();
var priceTags = document.DocumentNode.SelectNodes(textBox2.Text);     

然后我做一个foreach循环来迭代找到的价格并将它们添加到名为 productPriceList

的列表中
foreach (var prices in priceTags)
{
    label9.Visible = true;
    label9.Text += prices.InnerHtml + "\n";
    productPriceList.Add(prices.InnerHtml);
    label2.Visible = false;
    label3.Visible = false;
}

然后我将值添加到数据库

using (var con = new SqlConnection(connectionString))
{
    con.Open();
    using (var cmd = new SqlCommand(@"INSERT INTO OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, DateCaptured)
        VALUES(@CompetitorID, @ProductCode, @ProductName, @DateCaptured)", con))
    {
        cmd.Parameters.Add("@CompetitorID", SqlDbType.Int);
        cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar);
        cmd.Parameters.Add("@ProductName", SqlDbType.VarChar);
        cmd.Parameters.Add("@Price", SqlDbType.Decimal);
        cmd.Parameters.Add("@DateCaptured", SqlDbType.DateTime);
        for (int i = 0; i < competitorList.Count; i++)
        {
            cmd.Parameters["@CompetitorID"].Value = competitorList[i];
            cmd.Parameters["@ProductCode"].Value = productCodeList[i];
            cmd.Parameters["@ProductName"].Value = productNameList[i];
            cmd.Parameters["@Price"].Value = productPriceList[i];
            cmd.Parameters["@DateCaptured"].Value = dateCapturedList[i];
            int rowsAffected = cmd.ExecuteNonQuery();
        }

一切正常但我似乎找不到将InnerHtml值正确转换为Decimal类型的方法。有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:1)

根据您来自内部html的值,您可以执行类似这样的操作

using (var con = new SqlConnection(connectionString))
{
    con.Open();
     using (var cmd = new SqlCommand(@"INSERT INTO     OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, DateCaptured)
    VALUES(@CompetitorID, @ProductCode, @ProductName, @DateCaptured)", con))
    {
         cmd.Parameters.Add("@CompetitorID", SqlDbType.Int);
         cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar);
         cmd.Parameters.Add("@ProductName", SqlDbType.VarChar);
         cmd.Parameters.Add("@Price", SqlDbType.Decimal);
         cmd.Parameters.Add("@DateCaptured", SqlDbType.DateTime);
         for (int i = 0; i < competitorList.Count; i++)
         {
             cmd.Parameters["@CompetitorID"].Value = competitorList[i];
             cmd.Parameters["@ProductCode"].Value = productCodeList[i];
             cmd.Parameters["@ProductName"].Value = productNameList[i];
             cmd.Parameters["@Price"].Value = productPriceList[i];
             cmd.Parameters["@DateCaptured"].Value = dateCapturedList[i];
             int rowsAffected = cmd.ExecuteNonQuery(); // also the variable rowsAffected is not visible outside the scope of the for loop
         }
   }
}

<强>更新 然后,您应该在将值添加到列表中之前转换它们,如下所示:

foreach (var prices in priceTags)
{
   label9.Visible = true;
   label9.Text += prices.InnerHtml + "\n";
   productPriceList.Add(Decimal.Parse(prices.InnerHtml, <cultureInfoHere>));
   label2.Visible = false;
   label3.Visible = false;
}

通过这样做,您不必更改列表类型。