0

我不能选择...我不能在 go with dates recebe uma variavel que determina quantidade de meses a ser calculada MesesDemanda 中进行选择

for (int i = 1; i <= MesesDemanda; i++)
            {
                int cont = 0;
                DateTime dataposterior = DateTime.Now, dataanterior = DateTime.Now;
                dataanterior = dataanterior.AddMonths(i);
                dataposterior = dataposterior.AddMonths(-(i - 1));
                con.Open();
                cmd = new OleDbCommand("SELECT HistSaida.dataHistSaida, HistSaida.idProdutoHistSaida, HistSaida.qtdHistSaida FROM HistSaida WHERE (((HistSaida.dataHistSaida) BETWEEN #" + dataanterior + "# AND #" + dataposterior + "#) AND idProdutoHistSaida = " + idProduto + ")", con);
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Demanda = Demanda + Convert.ToInt32(dr["qtdHistSaida"]);
                        cont = 1;
                    }
                }
                dr.Close();
                con.Close();
                Valordivisao = Valordivisao + cont;
            }
4

1 回答 1

0

使用如下参数

cmd = new OleDbCommand("SELECT dataHistSaida, idProdutoHistSaida, qtdHistSaida FROM HistSaida WHERE ((dataHistSaida <=?  AND dataHistSaida >= ?) AND idProdutoHistSaida = ?)", con);

现在您可以设置如下值

cmd.Parameters.AddWithValue("@p1", dataanterior );
cmd.Parameters.AddWithValue("@p2", dataposterior );
cmd.Parameters.AddWithValue("@p3", idProduto );

如果您需要取qtdHistSaida列的总和,那么您可以如下更改 sql 语句

SELECT SUM(qtdHistSaida) FROM HistSaida WHERE ((dataHistSaida <=?  AND dataHistSaida >= ?) AND idProdutoHistSaida = ?)

现在您可以使用ExecuteScalar方法获取总和值,如下所示

var result  = cmd.ExecuteScalar();
于 2013-09-10T02:13:12.933 回答