将 InnerHtml 值转换为 Decimal
Convert InnerHtml values into Decimal
我正在尝试将 InnerHtml 值转换为十进制值。用户将在 textBox2
中输入一个 XPath
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 类型的方法。有办法实现吗?
根据你内心的价值观 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;
}
通过这样做,您不必更改列表类型。
我正在尝试将 InnerHtml 值转换为十进制值。用户将在 textBox2
中输入一个 XPathList<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 类型的方法。有办法实现吗?
根据你内心的价值观 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;
}
通过这样做,您不必更改列表类型。