简体   繁体   中英

How do you add parameters to a count query ran on an excel spreadsheet via oledb?

I am trying to count the number of values on a column that are before a certain date. The below is my current attempt. I am able to run this no problem without the where section, however of course this is needed for the correct value to be returned.

  1. string type = "Beacon Comm" this is the sheet name.
  2. TargetBuildOledbConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + FTPYTracker + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"
  3. string AssyColumn = "F" or whichever colum to search
  4. DateTimeSelection = "03/01/2020 00:00:00" or data from a date picker in the same format.

     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; } }

尝试这个

comm.Parameters.Add("@prod", OleDbType.VarChar).Value = lastDayOfMonth;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM