2014-05-09 21 views
0

我在C#下面的代碼來檢索表中的列:爲什麼有試圖從SQL表中選擇時錯誤

protected void Page_Load(object sender, EventArgs e) 
    { 

using (OleDbConnection connection = new OleDbConnection("Provider=MSDataShape;Data Provider=SQLOLEDB;" + 
    "Data Source=wmg-erp-db;Initial Catalog=DP;User ID=zh;Password=zhas")) 
{ 
OleDbDataAdapter adapter = new OleDbDataAdapter("SHAPE {SELECT FROM [DP].[dbo].[BT]} ", connection); 

DataSet customers = new DataSet(); 
adapter.Fill(customers, "Customers"); 
} 
    } 

我收到以下錯誤:

Exception Details: System.Data.OleDb.OleDbException: Incorrect syntax near the keyword 'FROM'. 

Source Error: 


Line 35: 
Line 36: DataSet customers = new DataSet(); 
Line 37: adapter.Fill(customers, "Customers"); 
Line 38: } 
Line 39: } 


Source File: c:\Webserver\WEXEC\Booking\booking.aspx.cs Line: 37 

Stack Trace: 


[OleDbException (0x80040e14): Incorrect syntax near the keyword 'FROM'.] 
    System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr) +60 
    System.Data.OleDb.OleDbDataReader.NextResult() +630 
    System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +546 
    System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +264 
    System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +9 
    System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +325 
    System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +420 
    System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +280 
    booking.Page_Load(Object sender, EventArgs e) in c:\Webserver\EXEC\Booking\booking.aspx.cs:37 
    System.Web.UI.Control.LoadRecursive() +71 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178 

如何我可以解決這個問題嗎?

+6

你忘了你的'SELECT'部分後指定列。像'SHAPE * {SELECT FROM [DP]。[dbo]。[BT]}' –

+0

DUH !!!我完全忽略了那一個。謝謝。 – SearchForKnowledge

回答

3

您錯過了在查詢語句中提及字段/列名稱。無論是在您的查詢提一些具體列名(OR)*這裏

new OleDbDataAdapter("SHAPE {SELECT FROM [DP].[dbo].[BT]} " 

線應該是

new OleDbDataAdapter("SHAPE {SELECT somecolumn FROM [DP].[dbo].[BT]} " 
2

您需要指定要選擇的內容。 正確的查詢將是 SELECT * FROM [DP].[dbo].[BT] 該查詢將返回所有記錄。

+0

完全忽略。謝謝 :) – SearchForKnowledge

2

SELECT * FROM*失蹤。

要麼指定列名,或*所有列

Refer

相關問題