如何使用存储整数值的字符串(SQL查询的结果)
本文关键字:SQL 查询 结果 字符串 存储 整数 何使用 | 更新日期: 2023-09-27 17:52:36
我正在使用c#和SQL Server。
我必须从表中检索特定产品的价格,并在以后的计算中使用这些值。
例如。
我使用下面的查询并将其存储在一个字符串中。
string str = " select cat_price from category where cat_itemID= 'A001'" ;
现在我需要将这里检索到的特定值存储在一个变量中以供进一步计算
例如。
int price_amount;
,我需要使用数学运算。我该怎么做呢?
使用SqlCommand
的ExecuteScalar
方法获取第一个值:
using (SqlConnection cn = new SqlConnection(...))
{
using (SqlCommand cmd = new SqlCommand(cn, "select cat_price from category where cat_itemID= 'A001'"))
{
//Execute the query and just get the first result.
int value = (int)cmd.ExecuteScalar();
}
}
如果这是您的问题,您可以使用SQLDbConnection和ExecuteScaler将此从DB中取出。下面是一个链接。
http://www.codeproject.com/KB/database/sql_in_csharp.aspx