2012-09-23 88 views
-1

我也跟着在這裏的教程:http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/錯誤時/選擇Excel列

但我只想查看某些列,所以我編輯查詢到

"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

虛擬場名稱F1F2等只是如果列標題行中沒有價值或使用,如果連接字符串包含HDR=No

如果您有標題行中的值,你必須HDR=Yes在連接字符串中,則需要使用實際的列標題而不是虛擬字段名稱,如F1