如何通过oledb在excel电子表格上运行的计数查询中添加参数?

问题描述 投票:-1回答:1

我正在尝试计算某个日期之前一列的值的数量。以下是我目前的尝试。没有where部分,我可以运行此问题,但是,当然需要返回正确的值。

  1. string type =“ Beacon Comm”,这是工作表名称。
  2. TargetBuildOledbConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + FTPYTracker + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"
  3. 字符串AssyColumn =“ F”或要搜索的任何列
  4. DateTimeSelection =“ 03/01/2020 00:00:00”或来自日期选择器的数据采用相同格式。

    public double GetInProgressOledb(string type, OleDbConnection FTPYBuildOleDbConnection, string AssyColumn, DateTime Selection)
    {
        const string prodParamName = "@prod";
        double lastDayOfMonth = (new DateTime(Selection.Year, Selection.Month, 1).AddMonths(1).AddDays(-1)).ToOADate();
        using (OleDbCommand comm = new OleDbCommand())
        {
            comm.Connection = FTPYBuildOleDbConnection;
            comm.CommandText = string.Format(ci, "SELECT Count(*) FROM [" + type + "$" + AssyColumn + ":" + AssyColumn + "] WHERE [" + AssyColumn + "$] = #" + lastDayOfMonth + "#");
            //comm.Parameters.Add(prodParamName, OleDbType.VarChar);
            //comm.Parameters[prodParamName].Value = lastDayOfMonth;
            double rowCount = Convert.ToDouble((int)comm.ExecuteScalar());
            return rowCount;
        }
    }
    
c# excel oledb oledbcommand
1个回答
0
投票

尝试一下

comm.Parameters.AddWithValue("@prodParamName", OleDbType.VarChar).Value = lastDayOfMonth;
© www.soinside.com 2019 - 2024. All rights reserved.