First, you should use parameterized SQL instead of putting this parameter directly in SQL. In addition, you must use the operator usingto close the command - and the connection - when you are done. Oh, and create a new one SqlConnectionfor each operation. So something like:
public int GetProductPrice(string productName)
{
using (var conn = new SqlConnection(...))
{
conn.Open();
using (var command = new SqlCommand(
"SELECT ProductPrice FROM Products WHERE ProductName = @ProductName",
conn))
{
command.AddParameter("@ProductName", SqlDbType.VarChar)
.Value = productName;
object price = command.ExecuteScalar();
}
}
}
ProductPrice. , long , , decimal. :
object tmp = cmd.ExecuteScalar();
... . - , . SqlDbType .