查询/选择excel列时出错

时间:2012-09-23 07:21:24

标签: c# excel oledb

我在这里按照教程:http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/

但我想只查看一些列,所以我将查询编辑成

"select F1,F3,F6 from [" + worksheetName + "$]", con);

但是我在网上遇到了这个错误

cmd.Fill(excelDataSet);


System.Data.OleDb.OleDbException was unhandled
  Message=No value given for one or more required parameters.
  Source=Microsoft Access Database Engine
  ErrorCode=-2147217904
  StackTrace:
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
       at ExcelReader.mainForm.GetWorksheet(String worksheetName) in D:\PLN\ExcelReader\ExcelReader\Main.cs:line 112
       at ExcelReader.mainForm.sheetsToolStripComboBox_SelectedIndexChanged(Object sender, EventArgs e) in D:\PLN\ExcelReader\ExcelReader\Main.cs:line 125
       at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
       at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
       at ExcelReader.mainForm.openToolStripButton_Click(Object sender, EventArgs e) in D:\PLN\ExcelReader\ExcelReader\Main.cs:line 51
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at ExcelReader.Program.Main() in D:\PLN\ExcelReader\ExcelReader\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

任何想法?

1 个答案:

答案 0 :(得分:1)

虚拟字段名称F1F2等仅在列标题行中没有值或连接字符串包含HDR=No

时使用

如果标题行中有值,并且连接字符串中有HDR=Yes,那么您需要使用实际的列标题,而不是像F1

这样的虚拟字段名称