使用 oledb 进行选择

本文关键字:选择 行选 oledb 使用 | 更新日期: 2023-09-27 18:30:41

我不能选择...我不能在日期内进行选择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;
            }

使用 oledb 进行选择

使用如下所示的参数

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();