保存在一个变量中并乘以double值查询结果C#/MySql

Save in a variable and multiply double value query result C#/MySql

我正在尝试将查询产生的唯一值保存到双精度变量中,因为它在数据库中存储为双精度,所以我以后可以将它与计数器相乘,如下所示代码:

 public partial class CoronaClaraCant : Form
 {
    private int conteoCliks = 0;
    private string producto = "CoronaClara";
    private double precio;

    public CoronaClaraCant()
    {
        InitializeComponent();  
    }

    private void CoronaClara_Click(object sender, EventArgs e)
    {
        MySqlConnection conn = new MySqlConnection("server=localhost;database=database;username=root;password=***"); 
        conn.Open();

        string select = "SELECT precio FROM productos where prodnom = '" + producto + "';";

        MySqlCommand cmd = new MySqlCommand(select, conn);
        double result = cmd.ExecuteNonQuery();
        conteoCliks++;
        precio = result;


        double total = (precio * conteoCliks);


        lblcantidad.Text = conteoCliks.ToString();
        lblprecio.Text = precio.ToString();

        lbltotal.Text = total.ToString();
    }
}

到目前为止我只得到负值:

我猜这可能是因为 ExecuteNonQuery,但我无法为 ExecuteScalar 方法分配双精度值,我不确定是否在将双精度值保存到变量中时,我还应该考虑其他参数。

您想从数据库中 select 数据并且您正在调用 ExecuteNonQuery 用于 插入、更新或删除命令.

您需要调用 ExecuteReader 或者如果您想在 DataTable 中获取数据,您可以填写 DataTable.

SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
   if (rdr.Read()) 
    {
      precio = (double)rdr["precio"];
    }
}