I am using C# and SQL Server.
I have to retrieve the prices of particular products from a table and make use of these values later for computation.
For eg.
I use the following query and store it in a string.
string str = " select cat_price from category where cat_item开发者_运维问答ID= 'A001'" ;
Now I need this particular value retrieved here to be stored in a variable for further computation
for eg.
int price_amount;
and I need to use mathematical operations. How do I do this?
Use the ExecuteScalar
method of a SqlCommand
to get the first value:
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();
}
}
You can use SQLDbConnection and ExecuteScaler to get get this out of the DB if that is your question. Below is a link to do this.
http://www.codeproject.com/KB/database/sql_in_csharp.aspx
精彩评论