简体   繁体   English

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

[英]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.如果没有 where 部分,我可以毫无问题地运行它,但是当然这需要返回正确的值。

  1. string type = "Beacon Comm" this is the sheet name. string type = "Beacon Comm" 这是工作表名称。
  2. TargetBuildOledbConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + FTPYTracker + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';" 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 string AssyColumn = "F" 或任何要搜索的列
  4. DateTimeSelection = "03/01/2020 00:00:00" or data from a date picker in the same format. 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; } }

尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过C#和OleDb在Excel电子表格中插入一行 - Inserting a row into Excel Spreadsheet via C# and OleDb 如何在每个行的Excel电子表格中向单元格添加表单控件 - How do you add a forms controls to a cell in a Excel spreadsheet for each row 使用OLEDB更新Excel电子表格 - Updating an excel spreadsheet using OLEDB 如何从 Excel 电子表格创建一个 .xslt 文件? - How do you create an .xslt file from an Excel spreadsheet? System.Data.OleDb.OleDbException:无效的Internet地址。如何使用OleDb连接到位于Web服务器上的Excel文件 - System.Data.OleDb.OleDbException: Invalid internet address. How do you connect to excel files located on a webserver using OleDb 如何在SSIS中以编程方式向Oledb Source添加参数 - How to add parameters to Oledb Source in ssis programmatically 如何以编程方式将XML Map添加到Excel 2010电子表格中? - How do I add an XML Map programmatically to an Excel 2010 Spreadsheet? 如何通过OleDB打开存储在byte []中的Excel文件? - How to open Excel file stored in byte[] via OleDB? 如何在C#中使用oledb仅上传非空行的Excel电子表格? - How to upload only non-empty rows of Excel spreadsheet using oledb in C#? 您如何根据列值从 excel 电子表格中 select 行并将其插入 dataGridViews? - How do you select rows from an excel spreadsheet based off of a column value and insert it into dataGridViews?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM