我試圖查詢CSV文件。它適用於我做一個簡單的選擇,但只要我嘗試添加一個where
子句,我碰到沒有給出一個或多個所需的參數值。Oledb查詢中的一個或多個必需參數沒有給出值
顯然,它聽起來像沒有得到提供的參數,但我試圖通過多種方式傳遞它。請參閱下面的一些代碼樣本
DateTime lastRunDate = Convert.ToDateTime(ConfigurationManager.AppSettings["LastRunDate"]);
OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + base.applicationRoot + ";" +
"Extended Properties=\"text;HDR=Yes;FMT=CSVDelimited\"");
// This works just fine
//OleDbDataAdapter adapter = new OleDbDataAdapter(String.Format("select * from {0}",
// This gives the error
OleDbDataAdapter adapter = new OleDbDataAdapter(String.Format("select top 100 * from [{0}] where {0}.sale_date = @sDate", base.csvFileName), conn);
//adapter.SelectCommand.Parameters.Add("@sDate", OleDbType.DBDate).Value = lastRunDate;
adapter.SelectCommand.Parameters.AddWithValue("@sDate", lastRunDate);
// This also gives the same error as above
//OleDbDataAdapter adapter = new OleDbDataAdapter(String.Format("select top 100 * from {0} where sale_date = '{1}'", base.csvFileName, lastRunDate), conn);
base.csvFileName, lastRunDate.ToShortDateString()), conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
感謝您的意見,但我已經嘗試過沒有前100名。 –